<?php

namespace App\Livewire\Events;

use App\Domain\Event\Enums\EventStatus;
use App\Domain\Event\Models\Event;
use App\Domain\Event\Services\EventService;
use App\Domain\Shared\Exceptions\DomainException;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Layout('layouts.app')]
#[Title('تفاصيل الحدث')]
class EventShow extends Component
{
    /**
     * Route-model bound, and Event carries BranchScope — another branch's event
     * cannot be resolved here. Locked so the browser cannot swap in a different
     * id after mount() has authorised this one.
     */
    #[Locked]
    public Event $event;

    public function mount(Event $event): void
    {
        $this->authorize('events.view');
        $this->event = $event->load(['facility', 'creator', 'cover', 'gallery']);
    }

    public function changeStatus(string $status, EventService $service): void
    {
        $this->authorize('events.manage');

        try {
            $newStatus = EventStatus::from($status);
            $service->changeStatus($this->event, $newStatus, auth()->user());
            $this->event->refresh();
            session()->flash('success', __('تم تغيير حالة الحدث'));
        } catch (DomainException $e) {
            session()->flash('error', $e->getMessage());
        }
    }

    public function publish(EventService $service): void
    {
        $this->authorize('events.manage');

        try {
            $service->publish($this->event, auth()->user());
            $this->event->refresh();
            session()->flash('success', __('تم نشر الحدث بنجاح'));
        } catch (DomainException $e) {
            session()->flash('error', $e->getMessage());
        }
    }

    public function openRegistration(): void
    {
        $this->authorize('events.manage');
        $this->event->update(['registration_closes_at' => null]);
        if ($this->event->registration_opens_at && $this->event->registration_opens_at->isFuture()) {
            $this->event->update(['registration_opens_at' => now()]);
        }
        $this->event->refresh();
        session()->flash('success', __('تم فتح التسجيل'));
    }

    public function closeRegistration(): void
    {
        $this->authorize('events.manage');
        $this->event->update(['registration_closes_at' => now()]);
        $this->event->refresh();
        session()->flash('success', __('تم إغلاق التسجيل'));
    }

    public function deleteEvent(EventService $service): void
    {
        $this->authorize('events.manage');

        try {
            $service->delete($this->event);
            session()->flash('success', __('تم حذف الحدث'));
            $this->redirect(route('events.list'), navigate: true);
        } catch (DomainException $e) {
            session()->flash('error', $e->getMessage());
        }
    }

    public function render()
    {
        return view('livewire.events.event-show', [
            'confirmedCount' => $this->event->registrations()->where('status', 'confirmed')->count(),
            'pendingCount' => $this->event->registrations()->where('status', 'pending')->count(),
        ]);
    }
}
