<?php

namespace Tests\Feature;

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

class PermissionSystemTest extends TestCase
{
    use RefreshDatabase;

    public function setUp(): void
    {
        parent::setUp();
        
        // Seed permissions and roles
        $this->artisan('db:seed', ['--class' => 'PermissionSeeder']);
    }

    public function test_super_admin_can_access_all_routes()
    {
        $user = User::factory()->create();
        $superAdminRole = Role::findByName('Super Admin');
        $user->assignRole($superAdminRole);

        $this->actingAs($user);

        // Test dashboard access
        $response = $this->get('/dashboard');
        $response->assertStatus(200);

        // Test user management access
        $response = $this->get('/users');
        $response->assertStatus(200);

        // Test folder management access
        $response = $this->get('/folders');
        $response->assertStatus(200);
    }

    public function test_regular_user_cannot_access_admin_routes()
    {
        $user = User::factory()->create();
        $userRole = Role::findByName('User');
        $user->assignRole($userRole);

        $this->actingAs($user);

        // Test user management access (should be forbidden)
        $response = $this->get('/users');
        $response->assertStatus(403);

        // Test dashboard access (should be allowed)
        $response = $this->get('/dashboard');
        $response->assertStatus(200);
    }

    public function test_manager_has_limited_access()
    {
        $user = User::factory()->create();
        $managerRole = Role::findByName('Manager');
        $user->assignRole($managerRole);

        $this->actingAs($user);

        // Test user management access (should be allowed)
        $response = $this->get('/users');
        $response->assertStatus(200);

        // Test dashboard access (should be allowed)
        $response = $this->get('/dashboard');
        $response->assertStatus(200);
    }

    public function test_unauthorized_user_cannot_access_protected_routes()
    {
        // Test without authentication
        $response = $this->get('/dashboard');
        $response->assertRedirect('/login');

        $response = $this->get('/users');
        $response->assertRedirect('/login');
    }
}