<?php

namespace Tests\Feature\Auth;

use App\Models\User;
use App\Notifications\TwoFactorLoginCode;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Tests\TestCase;

class TwoFactorAuthenticationTest extends TestCase
{
    use RefreshDatabase;

    public function test_enabled_two_factor_login_redirects_to_challenge(): void
    {
        Notification::fake();
        $this->seed(DatabaseSeeder::class);

        $user = User::where('email', 'admin@barberia-demo.test')->firstOrFail();
        $user->forceFill([
            'two_factor_secret' => Str::random(40),
            'two_factor_confirmed_at' => now(),
        ])->save();

        $this->post(route('login'), [
            'email' => $user->email,
            'password' => 'password',
        ])->assertRedirect(route('two-factor.login'));

        $this->assertGuest();
        Notification::assertSentTo($user, TwoFactorLoginCode::class);
    }
}
