<?php

namespace App\Domain\Notification\Listeners;

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

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

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

        if (!$participant) {
            return;
        }

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

        $this->pushService->sendToParticipantGuardians(
            $participant,
            'تقييم جديد',
            "تم مشاركة تقييم جديد لـ {$name}",
            [
                'type' => 'evaluation',
                'route' => '/participants/{participant_uuid}/evaluations/{evaluation_id}',
                'participant_uuid' => $participant->uuid,
                'evaluation_id' => (string) $evaluation->id,
            ],
            'evaluation.shared'
        );
    }
}
