<?php

namespace Tests\Unit\Notification;

use App\Domains\Notification\Events\PasswordResetRequested;
use App\Domains\Notification\Listeners\SendPasswordResetEmail;
use App\Domains\Notification\Services\NotificationMailService;
use Illuminate\Support\Facades\Log;
use RuntimeException;
use Tests\TestCase;

class SendPasswordResetEmailTest extends TestCase
{
    public function test_it_logs_and_rethrows_mail_failures(): void
    {
        $exception = new RuntimeException('SMTP unavailable.');
        $mailService = \Mockery::mock(NotificationMailService::class);
        $mailService->shouldReceive('sendPasswordResetCode')
            ->once()
            ->with(10, 'tenant-test')
            ->andThrow($exception);
        $this->app->instance(NotificationMailService::class, $mailService);

        Log::shouldReceive('error')
            ->once()
            ->with(
                'Failed to send password reset email.',
                \Mockery::on(fn (array $context): bool => $context['attempt_id'] === 10
                    && $context['tenant_code'] === 'tenant-test'
                    && $context['exception'] === $exception),
            );

        $this->expectExceptionObject($exception);

        (new SendPasswordResetEmail)->handle(
            new PasswordResetRequested(10, 'tenant-test'),
        );
    }
}
