<?php

namespace Tests\Feature\Auth;

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

class EmployeeAuthTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        // Seed roles
        $this->seed(\Database\Seeders\RoleSeeder::class);
    }

    public function test_employee_can_view_login_page()
    {
        $response = $this->get('/login');
        $response->assertStatus(200);
        $response->assertInertia(fn ($page) => $page->component('Guest/Pages/Login'));
    }

    public function test_employee_can_register()
    {
        $response = $this->post('/register', [
            'name' => 'Test Employee',
            'email' => 'employee@example.com',
            'password' => 'password',
            'password_confirmation' => 'password',
        ]);

        $this->assertAuthenticated();
        $response->assertRedirect(route('employee.dashboard'));

        $user = User::where('email', 'employee@example.com')->first();
        $this->assertTrue($user->role->slug === 'employee');
    }

    public function test_employee_can_login()
    {
        $role = Role::where('slug', 'employee')->first();
        $user = User::factory()->create([
            'password' => bcrypt('password'),
            'role_id' => $role->id,
        ]);

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

        $this->assertAuthenticatedAs($user);
        $response->assertRedirect(route('employee.dashboard'));
    }
}
