<?php

namespace App\Domain\Notification\Listeners;

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

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

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

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

        $participant = Participant::find($invoice->billable_id);
        if (!$participant) {
            return;
        }

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

        $this->pushService->sendToParticipantGuardians(
            $participant,
            'فاتورة متأخرة',
            "فاتورة بقيمة {$amount} ج.م تجاوزت تاريخ الاستحقاق",
            [
                'type' => 'invoice_overdue',
                'route' => '/participants/{participant_uuid}/invoices/{invoice_uuid}',
                'participant_uuid' => $participant->uuid,
                'invoice_uuid' => $invoice->uuid,
                'amount' => $invoice->due_amount,
            ],
            'invoice.overdue'
        );
    }
}
