<?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         = '';

    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',
        ];
    }

    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(),
        ]);

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

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

    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>';
    }
}
