<?php

namespace App\Domain\Financial\Listeners;

use App\Domain\Financial\Events\WalletDeposited;
use App\Domain\Notification\Services\NotificationService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;

class SendWalletDepositNotification implements ShouldQueue
{
    public function __construct(
        private NotificationService $notificationService,
    ) {}

    public function handle(WalletDeposited $event): void
    {
        try {
            $this->notificationService->sendSimple(
                type: 'wallet_deposited',
                recipientId: $event->wallet->participant_id,
                recipientType: 'participant',
                data: [
                    'participant_name' => $event->wallet->participant?->person?->full_name ?? '',
                    'amount' => number_format($event->amount / 100, 2),
                    'new_balance' => number_format($event->wallet->balance / 100, 2),
                ],
            );
        } catch (\Throwable $e) {
            Log::error('SendWalletDepositNotification failed: ' . $e->getMessage(), [
                'wallet_id' => $event->wallet->id,
            ]);
        }
    }

    public function failed(WalletDeposited $event, \Throwable $exception): void
    {
        Log::critical('SendWalletDepositNotification PERMANENTLY FAILED', [
            'wallet_id' => $event->wallet->id,
            'error' => $exception->getMessage(),
        ]);
    }
}
