<?php

namespace App\Domain\Notification\Listeners;

use App\Domain\Notification\Services\PushNotificationService;
use App\Domain\Participant\Models\Participant;
use App\Domain\Training\Events\WaitlistSpotAvailable;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendWaitlistSpotPush implements ShouldQueue
{
    public function __construct(private PushNotificationService $pushService) {}

    public function handle(WaitlistSpotAvailable $event): void
    {
        $waitlist = $event->waitlist;
        $group = $event->group;

        $participant = Participant::find($waitlist->participant_id);
        if (!$participant) {
            return;
        }

        $groupName = $group->name_ar ?? 'المجموعة';

        $this->pushService->sendToParticipantGuardians(
            $participant,
            'مكان متاح!',
            "تم توفر مكان في {$groupName} — يمكنك الآن إتمام التسجيل",
            [
                'type' => 'waitlist_spot',
                'route' => '/participants/{participant_uuid}/enrollments',
                'participant_uuid' => $participant->uuid,
                'group_id' => $group->id,
            ],
            'waitlist.spot_available'
        );
    }
}
