<?php

namespace App\Components\Dsar;

use App\Models\Dsar\Dsar;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;

class ChaseLogger extends Component {
    use AuthorizesRequests;

    public Dsar $dsar;

    public string $notes = '';

    protected function rules(): array {
        return [
            'notes' => 'nullable|string|max:2000',
        ];
    }

    public function recordChase(): void {
        $this->authorize('dsars.responses.log');
        $this->validate();

        $this->dsar->chases()->create([
            'user_id'   => auth()->id(),
            'notes'     => trim($this->notes) ?: null,
            'chased_at' => now(),
        ]);

        $this->reset('notes');

        $this->dispatch('toast', message: 'Chase recorded.', type: 'success');
    }

    public function render() {
        return view('components.dsar.chase-logger', [
            'chases' => $this->dsar->chases()->with('user')->latest('chased_at')->get(),
        ]);
    }
}
