<?php

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

uses(RefreshDatabase::class);

beforeEach(function () {
    $this->admin = User::factory()->create(['is_admin' => true]);
    $this->user = User::factory()->create(['is_admin' => false]);
});

describe('User Management Page', function () {
    
    it('allows admin users to access the user management page', function () {
        $this->actingAs($this->admin);
        
        $response = $this->get(route('admin.users'));
        
        $response->assertStatus(200);
        $response->assertSee('User Management');
        $response->assertSee('Users');
        $response->assertSee('Invitations');
    });
    
    it('prevents non-admin users from accessing the user management page', function () {
        $this->actingAs($this->user);
        
        $response = $this->get(route('admin.users'));
        
        $response->assertStatus(403);
    });
    
    it('redirects unauthenticated users to login', function () {
        $response = $this->get(route('admin.users'));
        
        $response->assertRedirect(route('login'));
    });
    
    it('shows the user management navigation link only to admins', function () {
        // Test admin sees the link
        $this->actingAs($this->admin);
        $response = $this->get(route('dashboard'));
        $response->assertSee('User Management');
        
        // Test regular user doesn't see the link in navigation
        $this->actingAs($this->user);
        $response = $this->get(route('dashboard'));
        $response->assertDontSee('href="' . route('admin.users') . '"', false);
    });
    
    it('properly displays the page title', function () {
        $this->actingAs($this->admin);
        
        $response = $this->get(route('admin.users'));
        
        $response->assertSee('<title>User Management</title>', false);
    });
});