<?php

namespace App\Domain\Notification\Listeners;

use App\Domain\Financial\Events\PaymentReceived;
use App\Domain\Notification\Services\PushNotificationService;
use App\Domain\Participant\Models\Participant;
use Illuminate\Contracts\Queue\ShouldQueue;

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

    public function handle(PaymentReceived $event): void
    {
        $payment = $event->payment;
        $invoice = $payment->invoice;

        if (!$invoice) {
            return;
        }

        $amount = number_format($payment->amount / 100, 2);

        if ($invoice->billable_type === 'App\\Domain\\Participant\\Models\\Participant') {
            $participant = Participant::find($invoice->billable_id);
            if (!$participant) {
                return;
            }

            $this->pushService->sendToParticipantGuardians(
                $participant,
                'تأكيد الدفع',
                "تم تأكيد دفع {$amount} ج.م بنجاح",
                [
                    'type' => 'payment',
                    'route' => '/participants/{participant_uuid}/invoices/{invoice_uuid}',
                    'participant_uuid' => $participant->uuid,
                    'invoice_uuid' => $invoice->uuid,
                    'amount' => (string) $payment->amount,
                ],
                'payment.confirmed'
            );
        }
    }
}
