<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AuthTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_can_login_with_valid_credentials(): void
    {
        $user = User::factory()->admin()->create(['email' => 'admin@bth.local', 'password' => 'password']);

        $response = $this->post('/login', ['email' => 'admin@bth.local', 'password' => 'password']);
        $response->assertRedirect(route('dashboard'));
        $this->assertAuthenticatedAs($user);
    }

    public function test_login_fails_with_invalid_password(): void
    {
        User::factory()->admin()->create(['email' => 'admin@bth.local', 'password' => 'password']);

        $response = $this->post('/login', ['email' => 'admin@bth.local', 'password' => 'wrong']);
        $response->assertRedirect();
        $this->assertGuest();
    }

    public function test_guest_is_redirected_to_login(): void
    {
        $response = $this->get('/dashboard');
        $response->assertRedirect('/login');
    }
}
