<?php

namespace App\Components\Claim\Application;

use App\Models\Claim\Claim;
use App\Services\RedressEstimateService;
use Livewire\Component;

class ProvisionalValue extends Component
{
    public Claim $claim;

    public function generate(RedressEstimateService $service)
    {
        $this->authorize('claims.edit');

        if ($this->claim->careSettings()->whereNotNull('statement')->doesntExist()) {
            $this->dispatch('toast', message: 'Please add at least one care setting statement first.', type: 'error');
            return;
        }

        if (!$this->claim->impactDetail) {
            $this->dispatch('toast', message: 'Please complete Impact Factors first.', type: 'error');
            return;
        }

        $service->estimate($this->claim);

        $this->claim->refresh();

        $this->dispatch('toast', message: 'Provisional value generated.', type: 'success');
    }

    public function render()
    {
        $this->claim->loadMissing('redressEstimate');

        return view('components.claim.application.provisional-value');
    }
}