<?php

declare(strict_types=1);

use App\Models\User;
use Database\Seeders\RoleAndPermissionSeeder;

beforeEach(function (): void {
    $this->seed(RoleAndPermissionSeeder::class);
    $this->accountUrl = 'http://'.config('app.domains.account');
});

it('renders the login page', function (): void {
    $this->get($this->accountUrl.'/login')
        ->assertOk();
});

it('authenticates a user with valid credentials', function (): void {
    $user = User::factory()->create();

    $this->post($this->accountUrl.'/login', [
        'email' => $user->email,
        'password' => 'password',
    ])->assertRedirect();

    $this->assertAuthenticated();
});

it('rejects invalid credentials', function (): void {
    $user = User::factory()->create();

    $this->post($this->accountUrl.'/login', [
        'email' => $user->email,
        'password' => 'wrong-password',
    ]);

    $this->assertGuest();
});

it('logs a user out', function (): void {
    $user = User::factory()->create();

    $this->actingAs($user)
        ->post($this->accountUrl.'/logout')
        ->assertRedirect();

    $this->assertGuest();
});
