<?php

namespace App\Domain\Financial\Listeners;

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

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

    public function handle(WalletFrozen $event): void
    {
        try {
            $this->notificationService->sendSimple(
                type: 'wallet_frozen',
                recipientId: $event->wallet->participant_id,
                recipientType: 'participant',
                data: [
                    'participant_name' => $event->wallet->participant?->person?->full_name ?? '',
                    'reason' => $event->wallet->freeze_reason ?? 'قرار إداري',
                ],
                priority: 'high',
            );
        } catch (\Throwable $e) {
            Log::error('SendWalletFrozenNotification failed: ' . $e->getMessage(), [
                'wallet_id' => $event->wallet->id,
            ]);
        }
    }

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