<?php

namespace App\Domain\Notification\Listeners;

use App\Domain\Attendance\Events\AttendanceThresholdBreached;
use App\Domain\Notification\Services\PushNotificationService;
use Illuminate\Contracts\Queue\ShouldQueue;

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

    public function handle(AttendanceThresholdBreached $event): void
    {
        $participant = $event->participant;
        $rate = round($event->rate, 1);
        $threshold = round($event->threshold, 1);

        $name = $participant->person?->name_ar ?? 'المشترك';

        $this->pushService->sendToParticipantGuardians(
            $participant,
            'تنبيه نسبة الحضور',
            "نسبة حضور {$name} ({$rate}%) أقل من الحد الأدنى ({$threshold}%)",
            [
                'type' => 'attendance_threshold',
                'route' => '/participants/{participant_uuid}/attendance',
                'participant_uuid' => $participant->uuid,
                'rate' => $rate,
                'threshold' => $threshold,
            ],
            'attendance.threshold_breached'
        );
    }
}
