<?php

namespace Tests\Feature;

use App\Models\User;
use Database\Seeders\PermissionSeeder;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ModuleAuthorizationTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed([RoleSeeder::class, PermissionSeeder::class]);
    }

    public function test_guest_is_redirected_from_clients(): void
    {
        $this->get(route('clients.index'))->assertRedirect(route('login'));
    }

    public function test_user_without_role_cannot_view_clients(): void
    {
        $user = User::factory()->create();

        $this->actingAs($user)
            ->get(route('clients.index'))
            ->assertForbidden();
    }

    public function test_cajero_can_list_clients_but_not_create_form(): void
    {
        $user = User::factory()->create();
        $user->assignRole('cajero');

        $this->actingAs($user)
            ->get(route('clients.index'))
            ->assertOk();

        $this->actingAs($user)
            ->get(route('clients.create'))
            ->assertForbidden();
    }

    public function test_administrador_can_access_clients_products_and_pos(): void
    {
        $user = User::factory()->create();
        $user->assignRole('administrador');

        $this->actingAs($user)->get(route('clients.index'))->assertOk();
        $this->actingAs($user)->get(route('products.index'))->assertOk();
        $this->actingAs($user)->get(route('pos.index'))->assertOk();
    }

    public function test_medico_cannot_use_pos_without_permission(): void
    {
        $user = User::factory()->create();
        $user->assignRole('medico');

        $this->actingAs($user)
            ->get(route('pos.index'))
            ->assertForbidden();
    }
}
