<?php

namespace App\Components\WillsCase;

use App\Models\WillsCase\WillsCase;
use Livewire\Component;

class StatusToggle extends Component {
    public WillsCase $willsCase;

    public function mount(WillsCase $willsCase): void {
        $this->willsCase = $willsCase;
    }

    public function open(): void {
        $this->willsCase->update([
            'is_open'   => true,
            'is_closed' => false,
            'closed_at' => null,
        ]);

        $this->willsCase = $this->willsCase->fresh();
        $this->dispatch('toast', type: 'success', message: 'Case marked as open.');
    }

    public function close(): void {
        $this->willsCase->update([
            'is_open'   => false,
            'is_closed' => true,
            'closed_at' => now(),
        ]);

        $this->willsCase = $this->willsCase->fresh();
        $this->dispatch('toast', type: 'success', message: 'Case marked as closed.');
    }

    public function render(): \Illuminate\View\View {
        return view('components.wills-case.status-toggle');
    }
}
