<?php

namespace App\Components\PensionCase;

use App\Models\LeadSource\LeadSource;
use App\Models\PensionCase\PensionCase;
use App\Services\PensionCaseService;
use Livewire\Attributes\Lazy;
use Livewire\Component;

#[Lazy]
class Edit extends Component {
    public PensionCase $pensionCase;

    // Case identity
    public string $sf_case_id = '';
    public string $case_number = '';
    public string $case_owner = '';
    public string $claims_writer = '';
    public string $file_investigator = '';

    // Claim details
    public string $claim_subject = '';
    public string $claim_basis = '';
    public string $priority = '';
    public string $underlying_investment = '';
    public string $investment_library = '';
    public string $pcp_subject = '';
    public string $additional_claims_info = '';
    public string $detail_of_outstanding_evidence = '';
    public string $reasons_for_not_proceeding = '';
    public string $client_settlement_instruction = '';

    // FSCS
    public string $fscs_contact_name = '';
    public string $fscs_contact_reference = '';
    public string $fscs_reference = '';

    // Statuses
    public string $settlement_status = '';
    public string $bank_claim_status = '';
    public string $fos_claim_status = '';
    public string $fscs_claim_status = '';

    // Client flags
    public bool $is_open = true;
    public bool $is_closed = false;
    public bool $vulnerable_client_flag = false;
    public string $vulnerable_client_details = '';
    public bool $special_contact_requirements_flag = false;
    public string $special_contact_requirements_details = '';
    public bool $sms_opt_out = false;
    public bool $do_not_contact = false;
    public bool $ifa_referred = false;
    public bool $ifa_refer_opted_out = false;
    public bool $client_in_dispute = false;
    public bool $sar_sent = false;
    public bool $sar_received = false;
    public bool $fee_capping_flag = false;
    public bool $atc_received = false;

    // Client identifiers
    public string $national_insurance = '';
    public string $cmc_client_reference = '';
    public string $car_reg = '';
    public string $atc = '';
    public string $tob_issued_by = '';
    public string $sar_to = '';
    public string $sar_received_from = '';
    public string $lead_source_cpf = '';
    public ?int $lead_source_id = null;
    public string $fee_structure = '';

    // Financials
    public string $settlement_to_date_raw = '';
    public string $settlement_to_date_new = '';
    public string $projected_future_settlement = '';
    public string $quantum = '';
    public string $cpfc_fee_percentage = '';
    public string $cpfc_fee_total_nett = '';
    public string $our_fees_percentage_raw = '';

    // Key dates
    public string $issued_tob_date = '';
    public string $tob_in_date = '';
    public string $ifa_referred_date = '';
    public string $application_submitted_date = '';
    public string $date_requested_evidence = '';
    public string $fos_allocated_date = '';
    public string $fos_app_submitted_date = '';
    public string $fos_app_written_sent_to_client_date = '';
    public string $fos_complaint_letter_sent_to_client_date = '';
    public string $fos_initial_complaint_sent_date = '';
    public string $fos_under_review_with_investigator_date = '';
    public string $fscs_allocated_date = '';
    public string $sar_received_date = '';
    public string $sar_reviewed_scanned_date = '';
    public string $application_out_for_signature_date = '';
    public string $allocated_to_investigator_date = '';

    public function mount(PensionCase $pensionCase): void {
        $this->pensionCase = $pensionCase;
        $this->fill($this->formFields());
    }

    protected function formFields(): array {
        $case = $this->pensionCase;
        return [
            'sf_case_id' => $case->sf_case_id ?? '',
            'case_number' => $case->case_number ?? '',
            'case_owner' => $case->case_owner ?? '',
            'claims_writer' => $case->claims_writer ?? '',
            'file_investigator' => $case->file_investigator ?? '',
            'claim_subject' => $case->claim_subject ?? '',
            'claim_basis' => $case->claim_basis ?? '',
            'priority' => $case->priority ?? '',
            'underlying_investment' => $case->underlying_investment ?? '',
            'investment_library' => $case->investment_library ?? '',
            'pcp_subject' => $case->pcp_subject ?? '',
            'additional_claims_info' => $case->additional_claims_info ?? '',
            'detail_of_outstanding_evidence' => $case->detail_of_outstanding_evidence ?? '',
            'reasons_for_not_proceeding' => $case->reasons_for_not_proceeding ?? '',
            'client_settlement_instruction' => $case->client_settlement_instruction ?? '',
            'fscs_contact_name' => $case->fscs_contact_name ?? '',
            'fscs_contact_reference' => $case->fscs_contact_reference ?? '',
            'fscs_reference' => $case->fscs_reference ?? '',
            'settlement_status' => $case->settlement_status ?? '',
            'bank_claim_status' => $case->bank_claim_status ?? '',
            'fos_claim_status' => $case->fos_claim_status ?? '',
            'fscs_claim_status' => $case->fscs_claim_status ?? '',
            'is_open' => $case->is_open ?? true,
            'is_closed' => $case->is_closed ?? false,
            'vulnerable_client_flag' => $case->vulnerable_client_flag ?? false,
            'vulnerable_client_details' => $case->vulnerable_client_details ?? '',
            'special_contact_requirements_flag' => $case->special_contact_requirements_flag ?? false,
            'special_contact_requirements_details' => $case->special_contact_requirements_details ?? '',
            'sms_opt_out' => $case->sms_opt_out ?? false,
            'do_not_contact' => $case->do_not_contact ?? false,
            'ifa_referred' => $case->ifa_referred ?? false,
            'ifa_refer_opted_out' => $case->ifa_refer_opted_out ?? false,
            'client_in_dispute' => $case->client_in_dispute ?? false,
            'sar_sent' => $case->sar_sent ?? false,
            'sar_received' => $case->sar_received ?? false,
            'fee_capping_flag' => $case->fee_capping_flag ?? false,
            'atc_received' => $case->atc_received ?? false,
            'national_insurance' => $case->national_insurance ?? '',
            'cmc_client_reference' => $case->cmc_client_reference ?? '',
            'car_reg' => $case->car_reg ?? '',
            'atc' => $case->atc ?? '',
            'tob_issued_by' => $case->tob_issued_by ?? '',
            'sar_to' => $case->sar_to ?? '',
            'sar_received_from' => $case->sar_received_from ?? '',
            'lead_source_cpf' => $case->lead_source_cpf ?? '',
            'lead_source_id' => $case->lead_source_id,
            'fee_structure' => $case->fee_structure ?? '',
            'settlement_to_date_raw' => $case->settlement_to_date_raw ?? '',
            'settlement_to_date_new' => $case->settlement_to_date_new ?? '',
            'projected_future_settlement' => $case->projected_future_settlement ?? '',
            'quantum' => $case->quantum ?? '',
            'cpfc_fee_percentage' => $case->cpfc_fee_percentage ?? '',
            'cpfc_fee_total_nett' => $case->cpfc_fee_total_nett ?? '',
            'our_fees_percentage_raw' => $case->our_fees_percentage_raw ?? '',
            'issued_tob_date' => $case->issued_tob_date?->format('Y-m-d') ?? '',
            'tob_in_date' => $case->tob_in_date?->format('Y-m-d') ?? '',
            'ifa_referred_date' => $case->ifa_referred_date?->format('Y-m-d') ?? '',
            'application_submitted_date' => $case->application_submitted_date?->format('Y-m-d') ?? '',
            'date_requested_evidence' => $case->date_requested_evidence?->format('Y-m-d') ?? '',
            'fos_allocated_date' => $case->fos_allocated_date?->format('Y-m-d') ?? '',
            'fos_app_submitted_date' => $case->fos_app_submitted_date?->format('Y-m-d') ?? '',
            'fos_app_written_sent_to_client_date' => $case->fos_app_written_sent_to_client_date?->format('Y-m-d') ?? '',
            'fos_complaint_letter_sent_to_client_date' => $case->fos_complaint_letter_sent_to_client_date?->format('Y-m-d') ?? '',
            'fos_initial_complaint_sent_date' => $case->fos_initial_complaint_sent_date?->format('Y-m-d') ?? '',
            'fos_under_review_with_investigator_date' => $case->fos_under_review_with_investigator_date?->format('Y-m-d') ?? '',
            'fscs_allocated_date' => $case->fscs_allocated_date?->format('Y-m-d') ?? '',
            'sar_received_date' => $case->sar_received_date?->format('Y-m-d') ?? '',
            'sar_reviewed_scanned_date' => $case->sar_reviewed_scanned_date?->format('Y-m-d') ?? '',
            'application_out_for_signature_date' => $case->application_out_for_signature_date?->format('Y-m-d') ?? '',
            'allocated_to_investigator_date' => $case->allocated_to_investigator_date?->format('Y-m-d') ?? '',
        ];
    }

    protected function rules(): array {
        return [
            'case_number' => 'nullable|string|max:255',
            'sf_case_id' => 'nullable|string|max:255',
            'case_owner' => 'nullable|string|max:255',
            'national_insurance' => 'nullable|string|max:20',
            'settlement_to_date_raw' => 'nullable|numeric',
            'settlement_to_date_new' => 'nullable|numeric',
            'projected_future_settlement' => 'nullable|numeric',
            'quantum' => 'nullable|numeric',
            'cpfc_fee_percentage' => 'nullable|numeric|min:0|max:100',
            'cpfc_fee_total_nett' => 'nullable|numeric',
            'our_fees_percentage_raw' => 'nullable|numeric|min:0|max:100',
            'lead_source_id' => 'nullable|exists:lead_sources,id',
        ];
    }

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

        $data = array_map(fn($v) => $v === '' ? null : $v, [
            'sf_case_id' => $this->sf_case_id,
            'case_number' => $this->case_number,
            'case_owner' => $this->case_owner,
            'claims_writer' => $this->claims_writer,
            'file_investigator' => $this->file_investigator,
            'claim_subject' => $this->claim_subject,
            'claim_basis' => $this->claim_basis,
            'priority' => $this->priority,
            'underlying_investment' => $this->underlying_investment,
            'investment_library' => $this->investment_library,
            'pcp_subject' => $this->pcp_subject,
            'additional_claims_info' => $this->additional_claims_info,
            'detail_of_outstanding_evidence' => $this->detail_of_outstanding_evidence,
            'reasons_for_not_proceeding' => $this->reasons_for_not_proceeding,
            'client_settlement_instruction' => $this->client_settlement_instruction,
            'fscs_contact_name' => $this->fscs_contact_name,
            'fscs_contact_reference' => $this->fscs_contact_reference,
            'fscs_reference' => $this->fscs_reference,
            'settlement_status' => $this->settlement_status,
            'bank_claim_status' => $this->bank_claim_status,
            'fos_claim_status' => $this->fos_claim_status,
            'fscs_claim_status' => $this->fscs_claim_status,
            'national_insurance' => $this->national_insurance,
            'cmc_client_reference' => $this->cmc_client_reference,
            'car_reg' => $this->car_reg,
            'atc' => $this->atc,
            'tob_issued_by' => $this->tob_issued_by,
            'sar_to' => $this->sar_to,
            'sar_received_from' => $this->sar_received_from,
            'lead_source_cpf' => $this->lead_source_cpf,
            'lead_source_id' => $this->lead_source_id ?: null,
            'fee_structure' => $this->fee_structure,
            'vulnerable_client_details' => $this->vulnerable_client_details,
            'special_contact_requirements_details' => $this->special_contact_requirements_details,
            'settlement_to_date_raw' => $this->settlement_to_date_raw,
            'settlement_to_date_new' => $this->settlement_to_date_new,
            'projected_future_settlement' => $this->projected_future_settlement,
            'quantum' => $this->quantum,
            'cpfc_fee_percentage' => $this->cpfc_fee_percentage,
            'cpfc_fee_total_nett' => $this->cpfc_fee_total_nett,
            'our_fees_percentage_raw' => $this->our_fees_percentage_raw,
            'issued_tob_date' => $this->issued_tob_date,
            'tob_in_date' => $this->tob_in_date,
            'ifa_referred_date' => $this->ifa_referred_date,
            'application_submitted_date' => $this->application_submitted_date,
            'date_requested_evidence' => $this->date_requested_evidence,
            'fos_allocated_date' => $this->fos_allocated_date,
            'fos_app_submitted_date' => $this->fos_app_submitted_date,
            'fos_app_written_sent_to_client_date' => $this->fos_app_written_sent_to_client_date,
            'fos_complaint_letter_sent_to_client_date' => $this->fos_complaint_letter_sent_to_client_date,
            'fos_initial_complaint_sent_date' => $this->fos_initial_complaint_sent_date,
            'fos_under_review_with_investigator_date' => $this->fos_under_review_with_investigator_date,
            'fscs_allocated_date' => $this->fscs_allocated_date,
            'sar_received_date' => $this->sar_received_date,
            'sar_reviewed_scanned_date' => $this->sar_reviewed_scanned_date,
            'application_out_for_signature_date' => $this->application_out_for_signature_date,
            'allocated_to_investigator_date' => $this->allocated_to_investigator_date,
        ]);

        // Booleans stay as booleans
        $data['is_open'] = $this->is_open;
        $data['is_closed'] = $this->is_closed;
        $data['vulnerable_client_flag'] = $this->vulnerable_client_flag;
        $data['special_contact_requirements_flag'] = $this->special_contact_requirements_flag;
        $data['sms_opt_out'] = $this->sms_opt_out;
        $data['do_not_contact'] = $this->do_not_contact;
        $data['ifa_referred'] = $this->ifa_referred;
        $data['ifa_refer_opted_out'] = $this->ifa_refer_opted_out;
        $data['client_in_dispute'] = $this->client_in_dispute;
        $data['sar_sent'] = $this->sar_sent;
        $data['sar_received'] = $this->sar_received;
        $data['fee_capping_flag'] = $this->fee_capping_flag;
        $data['atc_received'] = $this->atc_received;

        $service->update($this->pensionCase, $data);

        $this->dispatch('toast', title: 'Pension case updated', type: 'success');

        $this->redirectRoute('pension-cases.view', $this->pensionCase);
    }

    public function render() {
        $sarToOptions = $this->buildOptions(
            config('pension_case_options.sar_to', []),
            $this->sar_to,
        );
        $sarReceivedOptions = $this->buildOptions(
            config('pension_case_options.sar_received_from', []),
            $this->sar_received_from,
        );

        return view('components.pension-case.edit', [
            'sarToOptions' => $sarToOptions,
            'sarReceivedOptions' => $sarReceivedOptions,
            'leadSources' => LeadSource::pluck('display_name', 'id')->toArray(),
        ]);
    }

    private function buildOptions(array $values, ?string $current): array {
        if ($current && !in_array($current, $values, true)) {
            $values[] = $current;
        }

        sort($values, SORT_NATURAL | SORT_FLAG_CASE);

        return array_combine($values, $values);
    }

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