<?php

namespace App\Components\WillsCase;

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

#[Lazy]
class Edit extends Component {
    public WillsCase $willsCase;

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

    public function mount(WillsCase $willsCase): void {
        $this->willsCase         = $willsCase;
        $this->case_number       = $willsCase->case_number ?? '';
        $this->opened_at         = $willsCase->opened_at?->format('Y-m-d') ?? '';
        $this->closed_at         = $willsCase->closed_at?->format('Y-m-d') ?? '';
        $this->ifa_referred_date = $willsCase->ifa_referred_date?->format('Y-m-d') ?? '';
        $this->national_insurance = $willsCase->national_insurance ?? '';
        $this->ifa_referred      = (bool) $willsCase->ifa_referred;
        $this->is_open           = (bool) $willsCase->is_open;
        $this->fee_structure     = $willsCase->fee_structure ?? '';
        $this->occupation        = $willsCase->occupation ?? '';
    }

    protected function rules(): array {
        return [
            '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();

        $data = [
            'case_number'       => $this->case_number ?: null,
            'opened_at'         => $this->opened_at ?: null,
            '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,
        ];

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

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

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

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

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