<?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',
                'participant_uuid' => $participant->uuid,
                'enrollment_id' => $enrollment->id,
            ],
            'enrollment.created'
        );
    }
}
