<?php

namespace App\Components\Claim;

use App\Models\Claim\Claim;
use App\Models\Client\Client;
use App\Services\HistoryService;
use Livewire\Component;

class Edit extends Component {

    public $redress_types = [
        'FIXED' => 'Fixed Claim',
        'INDIVIDUAL' => 'Individual Claim',
    ];

    public Claim $claim;
    public Client $client;
    public $redress_type;
    public $case_number;

    public function mount() {
        $this->redress_type = $this->client->claim->redress_type;
        $this->case_number = $this->client->claim->case_number;
    }

    public function updateClaim(HistoryService $history): void {
        $data = $this->validate([
            'redress_type' => 'required|in:FIXED,INDIVIDUAL',
            'case_number'  => 'nullable|string|max:255',
        ]);

        $this->client->claim->update([
            'redress_type' => $data['redress_type'],
            'case_number'  => $data['case_number'],
        ]);

        $history->add($this->client, 'Claim updated — case number: "' . ($data['case_number'] ?: 'N/A') . '"', [
            'claim_id'    => $this->client->claim->id,
            'case_number' => $data['case_number'],
            'redress_type' => $data['redress_type'],
        ]);

        $this->redirectRoute('clients.show', $this->client);
    }

    public function render() {
        return view('components.claim.edit');
    }
}