<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use App\Http\Requests\Member\StorePartnerApplicationRequest;
use App\Mail\PartnerApplicationReceived;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;



class PartnerApplicationController extends Controller
{
    public function create()
    {
        return view('member.become_partner');
    }

    public function store(StorePartnerApplicationRequest $request)
    {
        $validated = $request->validated();

        DB::table('partners')->insert([
            'id' => (string) Str::uuid(),
            'name' => $validated['name'],
            'email' => $validated['email'],
            'phone' => $validated['phone'],
            //'contact_person' => $validated['contact_person'],
            'description' => $validated['description'] ?? null,
            'is_active' => 0, // يتم تركه غير نشط حتى يراجعه الأدمن ويوافق عليه
            'category_id' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
		
		$adminSetting = DB::table('settings')
            ->where('key', 'contact_email') 
            ->orWhere('key', 'admin_email')
            ->first();

        $adminEmail = $adminSetting ? $adminSetting->value : config('mail.from.address');

        // 🔥 إرسال الإيميل للأدمن
        if ($adminEmail) {
           @Mail::to($adminEmail)->send(new PartnerApplicationReceived($validated));
        }

        return redirect()->back()->with('success', 'Your application has been submitted successfully! Our team will review it shortly.');
    }
}
