<?php

namespace App\Components\Claim\Application;

use App\Models\Claim\Claim;
use App\Services\HistoryService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Livewire\Component;

class BankDetail extends Component {
    use AuthorizesRequests;
    public Claim $claim;

    public $isUnlocked = false;
    public $verificationPassword = '';

    public $name_on_account;
    public $sort_code;
    public $account_number;
    public $reference_number;
    public $bic_swift;
    public $clearing_code;
    public $iban;

    public function mount(Claim $claim) {
        $this->claim = $claim;
        $bankDetails = $claim->bankDetail;

        if ($bankDetails) {
            $this->name_on_account = $bankDetails->name_on_account;
            $this->sort_code = $bankDetails->sort_code;
            $this->account_number = $bankDetails->account_number;
            $this->reference_number = $bankDetails->reference_number;
            $this->bic_swift = $bankDetails->bic_swift;
            $this->clearing_code = $bankDetails->clearing_code;
            $this->iban = $bankDetails->iban;
        }
    }

    public function verifyPassword(HistoryService $history)
    {
        $this->validateOnly('verificationPassword', [
            'verificationPassword' => 'required'
        ]);

        if (Hash::check($this->verificationPassword, Auth::user()->password)) {
            $this->isUnlocked = true;
            $this->verificationPassword = '';

            $history->add($this->claim->client, "Bank details unlocked for viewing/editing.");
        } else {
            $this->addError('verificationPassword', 'The password is incorrect.');
            $this->dispatch('toast', message: 'Verification failed.', type: 'danger');

            $history->add($this->claim->client, "Failed attempt to unlock bank details.");
        }
    }

    public function saveBankDetails(HistoryService $history) {
        $this->authorize('claims.edit');
        $this->validate();

        $this->claim->bankDetail()->updateOrCreate(
            ['claim_id' => $this->claim->id],
            [
                'name_on_account' => $this->name_on_account,
                'sort_code' => $this->sort_code,
                'account_number' => $this->account_number,
                'reference_number' => $this->reference_number,
                'bic_swift' => $this->bic_swift,
                'clearing_code' => $this->clearing_code,
                'iban' => $this->iban,
            ]
        );

        $history->add($this->claim->client, "Bank details updated.", [
            'name_on_account' => $this->name_on_account,
            'sort_code' => $this->sort_code,
            'account_number' => $this->account_number,
            'reference_number' => $this->reference_number,
            'bic_swift' => $this->bic_swift,
            'clearing_code' => $this->clearing_code,
            'iban' => $this->iban,
        ]);

        $this->dispatch('toast', message: 'Bank details updated successfully!', type: 'success');
    }

    public function lockDetails()
    {
        $this->isUnlocked = false;
        $this->verificationPassword = '';
    }

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

    protected function rules() {
        return [
            'name_on_account' => 'required|string|max:255',
            'sort_code' => 'required|string|size:6',
            'account_number' => 'required|digits:8',
            'reference_number' => 'nullable|string|max:50',
            'bic_swift' => 'nullable|alpha_num|between:8,11',
            'clearing_code' => 'nullable|string|max:20',
            'iban' => 'nullable|string|max:34',
        ];
    }
}