<?php

namespace App\Livewire\Documents;

use App\Domain\Document\Enums\DocumentType;
use App\Domain\Document\Models\Document;
use App\Domain\Document\Services\DocumentService;
use App\Domain\Participant\Models\Participant;
use Livewire\Component;

class ParticipantDocuments extends Component
{
    public Participant $participant;

    public function mount(Participant $participant): void
    {
        $this->participant = $participant;
    }

    public function delete(string $uuid, DocumentService $service): void
    {
        $this->authorize('documents.delete');

        $document = Document::where('uuid', $uuid)->firstOrFail();
        $document->delete();

        session()->flash('success', __('تم حذف المستند'));
    }

    public function render()
    {
        $documents = Document::where('documentable_type', Participant::class)
            ->where('documentable_id', $this->participant->id)
            ->with('uploader')
            ->latest()
            ->get();

        return view('livewire.documents.participant-documents', [
            'documents' => $documents,
            'documentTypes' => DocumentType::cases(),
            'hasValidMedical' => app(DocumentService::class)->participantHasValidMedical($this->participant),
        ]);
    }
}
