<?php

namespace App\Domain\Notification\Listeners;

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

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

    public function handle(EnrollmentCreated $event): void
    {
        $enrollment = $event->enrollment;
        $participant = Participant::with('person')->find($enrollment->participant_id);

        if (!$participant) {
            return;
        }

        $name = $participant->person?->name_ar ?? 'المشترك';
        $group = $enrollment->group;
        $programName = $group?->program?->name_ar ?? 'البرنامج';

        $this->pushService->sendToParticipantGuardians(
            $participant,
            'تأكيد الاشتراك',
            "تم تسجيل {$name} في {$programName}",
            [
                'type' => 'enrollment',
                'route' => '/participants/{participant_uuid}/enrollments',
                'participant_uuid' => $participant->uuid,
                'enrollment_id' => (string) $enrollment->id,
            ],
            'enrollment.created'
        );
    }
}
