<?php

namespace App\Domain\Notification\Listeners;

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

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

    public function handle(InvoiceCreated $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->total_amount / 100, 2);

        $this->pushService->sendToParticipantGuardians(
            $participant,
            'فاتورة جديدة',
            "فاتورة بقيمة {$amount} ج.م بانتظار الدفع",
            [
                'type' => 'invoice',
                'participant_uuid' => $participant->uuid,
                'invoice_uuid' => $invoice->uuid,
                'amount' => $invoice->total_amount,
            ],
            'invoice.created'
        );
    }
}
