<?php

namespace App\Http\Controllers\Public;

use App\Http\Controllers\Controller;
use App\Http\Requests\LeadCaptureRequest;
use App\Models\Account;
use App\Models\ConsultantProfile;
use App\Models\Contact;
use App\Models\Lead;
use App\Models\Opportunity;
use App\Support\ActivityLogger;
use Illuminate\Http\RedirectResponse;

class LeadCaptureController extends Controller
{
    public function store(LeadCaptureRequest $request): RedirectResponse
    {
        $consultantProfile = null;
        $affiliateUserId = null;
        $ownerUserId = null;

        if ($request->filled('consultant_slug')) {
            $consultantProfile = ConsultantProfile::where('slug', $request->string('consultant_slug'))->first();
            $affiliateUserId = $consultantProfile?->user_id;
            $ownerUserId = $consultantProfile?->user_id;
        }

        $account = Account::firstOrCreate(
            ['name' => $request->input('company', $request->name)],
            [
                'website' => $request->website,
                'industry' => $request->industry,
                'phone' => $request->phone,
                'email' => $request->email,
            ]
        );

        $contact = Contact::firstOrCreate(
            ['email' => $request->email],
            [
                'account_id' => $account->id,
                'name' => $request->name,
                'phone' => $request->phone,
                'job_title' => 'Primary Contact',
            ]
        );

        $lead = Lead::create([
            'account_id' => $account->id,
            'primary_contact_id' => $contact->id,
            'owner_user_id' => $ownerUserId,
            'affiliate_user_id' => $affiliateUserId,
            'source' => $request->input('source', $consultantProfile ? 'affiliate_landing_page' : 'main_website_contact'),
            'stage' => 'new_lead',
            'name' => $request->name,
            'email' => $request->email,
            'phone' => $request->phone,
            'company' => $request->company,
            'website' => $request->website,
            'industry' => $request->industry,
            'pain_point' => $request->pain_point,
            'budget_range' => $request->budget_range,
            'urgency' => $request->urgency,
            'preferred_meeting_time' => $request->preferred_meeting_time,
            'payload' => $request->validated(),
        ]);

        Opportunity::create([
            'lead_id' => $lead->id,
            'account_id' => $account->id,
            'primary_contact_id' => $contact->id,
            'owner_user_id' => $ownerUserId,
            'affiliate_user_id' => $affiliateUserId,
            'stage' => 'qualified',
            'title' => ($request->company ?: $request->name).' Discovery Opportunity',
            'company_name' => $request->company ?: $request->name,
            'discovery_summary' => $request->pain_point,
        ]);

        ActivityLogger::log('lead.created', null, $lead, [], $lead->toArray(), $request);

        return back()->with('success', 'Thanks, your request has been received. Mosaic will follow up shortly.');
    }
}
