<?php

namespace App\Livewire\Documents;

use App\Domain\Document\DocumentSettings;
use App\Domain\Document\Services\DocumentService;
use App\Domain\Participant\Models\Participant;
use App\Domain\Shared\Models\SystemSetting;
use Livewire\Attributes\Locked;
use Livewire\Component;

class MedicalCertificateAlert extends Component
{
    /**
     * All server state, decided once in mount(): who the alert is about and
     * whether it fires. In Livewire v4 an unlocked public property is settable
     * from the page, which here would mean pointing the alert at another
     * player or simply switching off the "no valid medical certificate"
     * warning. Branch needs no check of its own — Participant carries the
     * strict BranchScope, so a player from another branch never binds.
     */
    #[Locked]
    public Participant $participant;

    #[Locked]
    public bool $show = false;

    #[Locked]
    public bool $isBlocking = false;

    #[Locked]
    public bool $hasValid = false;

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

        $requireMedical = SystemSetting::get(DocumentSettings::REQUIRE_MEDICAL_CERTIFICATE, false);
        if (!$requireMedical) {
            return;
        }

        $service = app(DocumentService::class);
        $this->hasValid = $service->participantHasValidMedical($participant);
        $this->isBlocking = (bool) SystemSetting::get(DocumentSettings::BLOCK_ATTENDANCE_WITHOUT_MEDICAL, false);
        $this->show = !$this->hasValid;
    }

    public function render()
    {
        return view('livewire.documents.medical-certificate-alert');
    }
}
