<?php

namespace App\Components\Dsar;

use App\Models\Dsar\Dsar;
use App\Services\DsarService;
use Livewire\Component;

class View extends Component {
    public Dsar $dsar;

    public function mount(): void {
        $this->authorize('dsars.view');
    }

    public function deleteDsar(DsarService $dsarService) {
        $this->authorize('delete', $this->dsar);

        $this->dsar->loadMissing('claim.client');
        $client = $this->dsar->claim->client;
        $institution = $this->dsar->institution?->display_name ?? 'this institution';

        $dsarService->delete($this->dsar);

        $this->dispatch('toast', message: "DSAR to \"{$institution}\" deleted.", type: 'success');

        return redirect()->route('clients.show', $client);
    }

    public function render() {
        $this->dsar->loadMissing(['institution', 'emailSend', 'latestResponse', 'claim.client']);

        return view('components.dsar.view', [
            'institution' => $this->dsar->institution,
            'emailSend'   => $this->dsar->emailSend,
        ]);
    }
}
