<?php

namespace App\Components\WillsCase;

use App\Models\Client\Client;
use App\Services\WillsCaseService;
use Livewire\Attributes\Lazy;
use Livewire\Component;

#[Lazy]
class Create extends Component {
    // Client fields
    public string $title      = '';
    public string $name_first = '';
    public string $name_last  = '';
    public string $name_middle = '';
    public string $phone_number   = '';
    public string $email_address  = '';
    public string $date_of_birth  = '';

    // Case fields
    public string $case_number        = '';
    public string $opened_at          = '';
    public string $closed_at          = '';
    public string $ifa_referred_date  = '';
    public string $national_insurance = '';
    public bool   $ifa_referred       = false;
    public bool   $is_open            = true;
    public string $fee_structure      = '';
    public string $occupation         = '';

    // New fields
    public string $rep              = '';
    public string $case_owner       = '';
    public array  $products         = [];
    public string $drafting_contact = '';
    public string $our_fee          = '';
    public string $awt_invoice_issued_at = '';
    public string $our_fee_status   = '';
    public string $external_draft_fee = '';
    public string $welcome_letter_sent_at = '';
    public string $docs_outstanding_from_reps = '';
    public string $introduction_call_at = '';
    public string $intro_call_by    = '';
    public string $trust_docs_instructed_at = '';
    public string $in_house_instructed_at = '';
    public string $drafts_received_at = '';
    public string $drafts_issued_to_client_at = '';
    public string $master_copies_signed_at = '';
    public string $master_copies_received_at = '';
    public string $date_on_document_signed = '';
    public ?bool  $documents_requiring_registration = null;
    public array  $documents_to_register_aiker = [];
    public array  $documents_to_register_external = [];
    public array  $registration_completed_aiker = [];
    public array  $registration_completed_external = [];
    public string $client_letters_from_hmrc_at = '';
    public string $annual_minutes_task_set_at = '';

    protected function rules(): array {
        return [
            'name_first'         => 'required|string|max:255',
            'name_last'          => 'required|string|max:255',
            'title'              => 'nullable|string|max:50',
            'name_middle'        => 'nullable|string|max:255',
            'phone_number'       => 'nullable|string|max:50',
            'email_address'      => 'nullable|email|max:255',
            'date_of_birth'      => 'nullable|date',
            'case_number'        => 'nullable|string|max:255',
            'opened_at'          => 'nullable|date',
            'closed_at'          => 'nullable|date',
            'ifa_referred_date'  => 'nullable|date',
            'national_insurance' => 'nullable|string|max:20',
            'ifa_referred'       => 'boolean',
            'is_open'            => 'boolean',
            'fee_structure'      => 'nullable|string|max:255',
            'occupation'         => 'nullable|string|max:255',
            'rep'                            => 'nullable|string|max:255',
            'case_owner'                     => 'nullable|string|max:50',
            'products'                       => 'nullable|array',
            'products.*'                     => 'string',
            'drafting_contact'               => 'nullable|string|max:255',
            'our_fee'                        => 'nullable|string|max:255',
            'awt_invoice_issued_at'          => 'nullable|date',
            'our_fee_status'                 => 'nullable|string|max:50',
            'external_draft_fee'             => 'nullable|string|max:255',
            'welcome_letter_sent_at'         => 'nullable|date',
            'docs_outstanding_from_reps'     => 'nullable|string',
            'introduction_call_at'           => 'nullable|date',
            'intro_call_by'                  => 'nullable|string|max:50',
            'trust_docs_instructed_at'       => 'nullable|date',
            'in_house_instructed_at'         => 'nullable|date',
            'drafts_received_at'             => 'nullable|date',
            'drafts_issued_to_client_at'     => 'nullable|date',
            'master_copies_signed_at'        => 'nullable|date',
            'master_copies_received_at'      => 'nullable|date',
            'date_on_document_signed'        => 'nullable|date',
            'documents_requiring_registration' => 'nullable|boolean',
            'documents_to_register_aiker'    => 'nullable|array',
            'documents_to_register_aiker.*'  => 'string',
            'documents_to_register_external'   => 'nullable|array',
            'documents_to_register_external.*' => 'string',
            'registration_completed_aiker'   => 'nullable|array',
            'registration_completed_external' => 'nullable|array',
            'client_letters_from_hmrc_at'    => 'nullable|date',
            'annual_minutes_task_set_at'     => 'nullable|date',
        ];
    }

    public function save(WillsCaseService $service): void {
        $this->validate();

        $client = Client::create([
            'title'         => $this->title ?: null,
            'name_first'    => $this->name_first,
            'name_last'     => $this->name_last,
            'name_middle'   => $this->name_middle ?: null,
            'phone_number'  => $this->phone_number ?: null,
            'email_address' => $this->email_address ?: null,
            'date_of_birth' => $this->date_of_birth ?: null,
        ]);

        $case = $service->create($client, [
            'case_number'        => $this->case_number ?: null,
            'opened_at'          => $this->opened_at ?: now(),
            'closed_at'          => $this->closed_at ?: null,
            'ifa_referred_date'  => $this->ifa_referred_date ?: null,
            'national_insurance' => $this->national_insurance ?: null,
            'ifa_referred'       => $this->ifa_referred,
            'is_open'            => $this->is_open,
            'is_closed'          => !$this->is_open,
            'fee_structure'      => $this->fee_structure ?: null,
            'occupation'         => $this->occupation ?: null,
            'created_by'         => auth()->id(),
            'rep'                            => $this->rep ?: null,
            'case_owner'                     => $this->case_owner ?: null,
            'products'                       => $this->products ?: null,
            'drafting_contact'               => $this->drafting_contact ?: null,
            'our_fee'                        => $this->our_fee ?: null,
            'awt_invoice_issued_at'          => $this->awt_invoice_issued_at ?: null,
            'our_fee_status'                 => $this->our_fee_status ?: null,
            'external_draft_fee'             => $this->external_draft_fee ?: null,
            'welcome_letter_sent_at'         => $this->welcome_letter_sent_at ?: null,
            'docs_outstanding_from_reps'     => $this->docs_outstanding_from_reps ?: null,
            'introduction_call_at'           => $this->introduction_call_at ?: null,
            'intro_call_by'                  => $this->intro_call_by ?: null,
            'trust_docs_instructed_at'       => $this->trust_docs_instructed_at ?: null,
            'in_house_instructed_at'         => $this->in_house_instructed_at ?: null,
            'drafts_received_at'             => $this->drafts_received_at ?: null,
            'drafts_issued_to_client_at'     => $this->drafts_issued_to_client_at ?: null,
            'master_copies_signed_at'        => $this->master_copies_signed_at ?: null,
            'master_copies_received_at'      => $this->master_copies_received_at ?: null,
            'date_on_document_signed'        => $this->date_on_document_signed ?: null,
            'documents_requiring_registration' => $this->documents_requiring_registration,
            'documents_to_register_aiker'    => $this->documents_to_register_aiker ?: null,
            'documents_to_register_external' => $this->documents_to_register_external ?: null,
            'registration_completed_aiker'   => $this->buildRegistrationDates($this->registration_completed_aiker, $this->documents_to_register_aiker),
            'registration_completed_external' => $this->buildRegistrationDates($this->registration_completed_external, $this->documents_to_register_external),
            'client_letters_from_hmrc_at'    => $this->client_letters_from_hmrc_at ?: null,
            'annual_minutes_task_set_at'     => $this->annual_minutes_task_set_at ?: null,
        ]);

        $this->dispatch('toast', title: 'Wills & Trusts case created', type: 'success');

        $this->redirectRoute('wills-cases.view', $case);
    }

    protected function buildRegistrationDates(array $datesIndexed, array $selectedDocs): ?array {
        $result = [];
        foreach (\App\Models\WillsCase\WillsCase::REGISTRATION_DOC_OPTIONS as $idx => $doc) {
            if (in_array($doc, $selectedDocs, true) && !empty($datesIndexed[$idx])) {
                $result[$doc] = $datesIndexed[$idx];
            }
        }
        return $result ?: null;
    }

    public function render() {
        return view('components.wills-case.create');
    }

    public function placeholder() {
        return '<div class="p-4"><div class="placeholder-glow"><span class="placeholder col-6 placeholder-lg"></span></div></div>';
    }
}
