<?php

namespace Tests\Feature;

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

class AdminUserDirectoryTest extends TestCase
{
    use RefreshDatabase;

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

    public function test_non_admin_cannot_access_user_directory(): void
    {
        $user = User::factory()->create(['is_admin' => false]);

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

    public function test_admin_views_paginated_user_directory(): void
    {
        $admin = User::factory()->admin()->create();

        User::factory()->create(['email' => 'inv@example.test']);

        $response = $this->actingAs($admin)->get(route('admin.users.index'));

        $response->assertOk();
        $response->assertInertia(fn ($page) => $page
            ->component('admin/users-directory')
            ->has('users.data')
            ->where('filters.filter', 'all'));

        $this->actingAs($admin)
            ->get(route('admin.users.index', ['q' => 'inv@example.test']))
            ->assertOk()
            ->assertInertia(fn ($page) => $page
                ->has('users.data', 1)
                ->where('users.data.0.email', 'inv@example.test'));
    }
}
