<?php

namespace App\Components\PensionCase;

use App\Models\Claim\Status;
use App\Models\PensionCase\PensionCase;
use App\Services\PensionCaseService;
use Livewire\Component;

class EditStatus extends Component {
    public PensionCase $pensionCase;

    public bool $showHistory = false;

    public ?int $status_id = null;

    public ?string $claim_type = null;

    public function mount(PensionCase $pensionCase): void {
        $this->pensionCase = $pensionCase;
        $this->status_id = $pensionCase->status_id;
        $this->claim_type = $pensionCase->claim_type;
    }

    // When the claim type is switched, the previously selected status belongs to
    // the old type's workflow, so clear it and force a fresh selection.
    public function updatedClaimType(): void {
        if ($this->claim_type !== $this->pensionCase->claim_type) {
            $this->status_id = null;
        } else {
            $this->status_id = $this->pensionCase->status_id;
        }
    }

    public function save(PensionCaseService $service): void {
        $typeChanged = $this->claim_type && $this->claim_type !== $this->pensionCase->claim_type;

        if (!$this->status_id) {
            $message = $typeChanged
                ? 'Select a status for the new claim type before changing.'
                : 'Please select a status.';
            $this->dispatch('toast', type: 'error', message: $message);
            return;
        }

        if ($typeChanged) {
            if (!$service->changeClaimType($this->pensionCase, $this->claim_type, $this->status_id)) {
                $this->dispatch('toast', type: 'error', message: 'Unable to change claim type.');
                return;
            }
            $this->refreshCase();
            $this->dispatch('toast', type: 'success', message: 'Claim type changed to ' . strtoupper($this->claim_type) . ' and status set.');
            return;
        }

        if (!$service->updateStatus($this->pensionCase, $this->status_id)) {
            $this->dispatch('toast', type: 'error', message: 'Status is already set to that value.');
            return;
        }

        $this->refreshCase();
        $this->dispatch('toast', type: 'success', message: 'Status updated.');
    }

    protected function refreshCase(): void {
        $this->pensionCase = $this->pensionCase->fresh(['status', 'statusHistory.status']);
        $this->status_id = $this->pensionCase->status_id;
        $this->claim_type = $this->pensionCase->claim_type;

        $this->dispatch('status-updated', pensionCaseId: $this->pensionCase->id);
    }

    public function render(): \Illuminate\View\View {
        // Drive the status list from the currently selected type, so switching the
        // type immediately shows that workflow's statuses.
        $type = 'pension-' . ($this->claim_type ?? $this->pensionCase->claim_type);

        return view('components.pension-case.edit-status', [
            'statuses' => Status::forType($type)->get(),
        ]);
    }
}
