<?php

namespace App\Domains\Notification\Listeners;

use App\Domains\Notification\Events\PasswordResetRequested;
use App\Domains\Notification\Services\NotificationMailService;
use Illuminate\Contracts\Queue\ShouldQueueAfterCommit;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use Throwable;

class SendPasswordResetEmail implements ShouldQueueAfterCommit
{
    use InteractsWithQueue;

    public string $queue = 'emails';

    public int $tries = 3;

    /** @var array<int, int> */
    public array $backoff = [30, 120, 300];

    public function handle(PasswordResetRequested $event): void
    {
        try {
            app(NotificationMailService::class)->sendPasswordResetCode(
                $event->attemptId,
                $event->tenantCode,
            );
        } catch (Throwable $exception) {
            Log::error('Failed to send password reset email.', [
                'attempt_id' => $event->attemptId,
                'tenant_code' => $event->tenantCode,
                'exception' => $exception,
            ]);

            throw $exception;
        }
    }
}
