<?php

namespace App\Components\Claim\Application;

use App\Models\Client\Client;
use App\Services\HistoryService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;

class ConfirmIdentity extends Component {
    use AuthorizesRequests;
    public Client $client;

    // Form properties
    public $isProvidingDocuments = null;
    public $primaryConfirmation = '';
    public $list1Confirmation = '';
    public $list2Confirmation = '';

    // Options for selects
    public $primaryOptions = [
        'passport' => 'Passport',
        'drivers_license' => 'Full or provisional driving licence (with a photo)',
        'national_id' => 'European Economic Area (EEA) national identity card',
        'brp' => 'Biometric Residence Permit (BRP)',
        'na' => 'Not Available',
    ];

    public $list1Options = [
        'utility_bill' => 'full version of your birth certificate',
        'marriage_cert' => 'marriage or civil partnership certificate',
        'drivers_license_paper' => 'UK driving licence without a photo (paper version)',
        'entitlement_card' => 'National Entitlement Card (Scotland only)',
        'identity_card' => 'identity photo card with PASS logo (this includes a Young Scot card)',
        'forces_card' => 'HM Forces ID card',
    ];

    public $list2Options = [
        'medical_letter' => 'official letter from a medical professional (for example the NHS or your doctor)',
        'utility_bill' => 'utility bill from the last 3 months',
        'financial_letter' => 'official letter from a financial institution (for example your bank)',
        'tenancy_agmt' => 'tenancy agreement (UK only)',
        'mortgage_stmt' => 'mortgage statement (UK only)',
        'tax_letter' => 'letter from HMRC or DWP (UK only)',
        'tax_stmt' => 'council tax statement (UK only)',
        'dwp_letter' => 'P45 or P60 (UK only)',
        'homeless_letter' => 'homeless registration letter from a local authority (UK only)',
    ];

    public function mount(Client $client) {
        $this->client = $client;

        $this->isProvidingDocuments = $this->client->confirmIdentity?->provides_docs;
        $this->primaryConfirmation = $this->client->confirmIdentity?->primary_doc_type;
        $this->list1Confirmation = $this->client->confirmIdentity?->list1_doc_type;
        $this->list2Confirmation = $this->client->confirmIdentity?->list2_doc_type;
    }

    public function save(HistoryService $history) {
        $this->authorize('claims.edit');
        $this->validate([
            'isProvidingDocuments' => 'required',
            'primaryConfirmation' => 'nullable|string',
            'list1Confirmation' => 'nullable|string',
            'list2Confirmation' => 'nullable|string',
        ]);

        $this->client->confirmIdentity()->updateOrCreate([
            'provides_docs' => $this->isProvidingDocuments,
            'primary_doc_type' => $this->primaryConfirmation,
            'list1_doc_type' => $this->list1Confirmation,
            'list2_doc_type' => $this->list2Confirmation,
        ]);

        $history->add($this->client, 'Identity confirmation updated.', [
            'provides_docs' => $this->isProvidingDocuments,
            'primary_doc_type' => $this->primaryConfirmation,
        ]);

        $this->dispatch('toast', type: 'success', message: 'Details saved.');
    }

    public function render() {
        return view('components.claim.application.confirm-identity');
    }
}
