<?php

use App\Jobs\SendPaymentFailedReminderJob;
use App\Mail\PaymentFailedDay3Reminder;
use App\Mail\PaymentFailedDay5Reminder;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;

uses(RefreshDatabase::class);

it('sends the day-3 mailable when grace_period_until is still set', function (): void {
    Mail::fake();
    $user = User::factory()->create(['grace_period_until' => now()->addDays(2)]);

    (new SendPaymentFailedReminderJob($user->id, 3))->handle();

    Mail::assertSent(PaymentFailedDay3Reminder::class, fn ($mail) => $mail->hasTo($user->email));
});

it('sends the day-5 mailable when grace_period_until is still set', function (): void {
    Mail::fake();
    $user = User::factory()->create(['grace_period_until' => now()->addDay()]);

    (new SendPaymentFailedReminderJob($user->id, 5))->handle();

    Mail::assertSent(PaymentFailedDay5Reminder::class, fn ($mail) => $mail->hasTo($user->email));
});

it('silently skips the day-3 mailable when grace has been cleared', function (): void {
    Mail::fake();
    $user = User::factory()->create(['grace_period_until' => null]);

    (new SendPaymentFailedReminderJob($user->id, 3))->handle();

    Mail::assertNothingSent();
});

it('silently skips the day-5 mailable when grace has been cleared', function (): void {
    Mail::fake();
    $user = User::factory()->create(['grace_period_until' => null]);

    (new SendPaymentFailedReminderJob($user->id, 5))->handle();

    Mail::assertNothingSent();
});

it('silently skips when the user no longer exists', function (): void {
    Mail::fake();

    (new SendPaymentFailedReminderJob(999999, 3))->handle();

    Mail::assertNothingSent();
});
