<?php

namespace Tests\Browser;

use Illuminate\Foundation\Testing\DatabaseTruncation;
use Laravel\Dusk\Browser;
use Tests\Browser\Concerns\AuthenticatesUsers;
use Tests\DuskTestCase;

class LoginTest extends DuskTestCase
{
    use DatabaseTruncation;
    use AuthenticatesUsers; 
        
    public function test_login(): void
    {
        $user = $this->createTestUser("login-test@example.com");

        $this->browse(function (Browser $browser) use ($user) {
            $this->loginUser($browser, $user);
            $this->assertWithDebugPause($browser, fn($b) => 
                $b->assertPathIs('/dashboard'),
                1000  // Custom pause time for this test
            );
        });
    }

    public function test_invalid_login(): void
    {
        $user = $this->createTestUser("invalid-email@example.com");

        $this->browse(function (Browser $browser) use ($user) {
            $this->loginUser($browser, $user);
            $this->assertWithDebugPause($browser, fn($b) => 
                $b->visit('/admin')
                    ->waitForLocation('/admin')
                    ->assertPathIs('/admin')
                    ->assertSee('FORBIDDEN'),
                1000  // Custom pause time for this test
            );
        });
    }

    public function test_access_admin_panel(): void
    {
        $user = $this->createTestUser("login-test@example.com");

        $this->browse(function (Browser $browser) use ($user) {
            $this->loginUser($browser, $user);
            $this->assertWithDebugPause($browser, fn($b) => 
                $b->visit('/admin')
                    ->waitForLocation('/admin')
                    ->assertPathIs('/admin')
                    ->assertTitleContains('Dashboard')
                    ->assertDontSee('FORBIDDEN'),
                1000  // Custom pause time for this test
            );
        });
    }
}