<?php

use App\Models\User;
use Livewire\Livewire;
use Modules\Identity\Database\Seeders\RolePermissionSeeder;
use Modules\Identity\Filament\Resources\Staff\Pages\ListStaff;
use Modules\Identity\Filament\Resources\Staff\StaffResource;

beforeEach(function () {
    $this->seed(RolePermissionSeeder::class);

    $this->superAdmin = User::factory()->create();
    $this->superAdmin->assignRole('super_admin');
});

test('a super_admin can list, create, and edit staff', function () {
    $this->actingAs($this->superAdmin)->get('/admin/staff')->assertSuccessful();
    $this->actingAs($this->superAdmin)->get('/admin/staff/create')->assertSuccessful();

    $other = User::factory()->create();
    $other->assignRole('support');

    $this->actingAs($this->superAdmin)->get("/admin/staff/{$other->id}/edit")->assertSuccessful();
});

test('an admin without manage_staff is forbidden from the staff resource', function () {
    $admin = User::factory()->create();
    $admin->assignRole('admin');

    $this->actingAs($admin)->get('/admin/staff')->assertForbidden();
});

test('the staff resource only lists users carrying an admin-tier role', function () {
    $support = User::factory()->create();
    $support->assignRole('support');

    $customer = User::factory()->create();

    $this->actingAs($this->superAdmin);

    Livewire::test(ListStaff::class)
        ->assertCanSeeTableRecords([$support])
        ->assertCanNotSeeTableRecords([$customer]);
});

test('a super_admin cannot delete their own staff account', function () {
    $this->actingAs($this->superAdmin);

    expect(StaffResource::canDelete($this->superAdmin))->toBeFalse();
});

test('a super_admin can delete another staff account', function () {
    $other = User::factory()->create();
    $other->assignRole('support');

    $this->actingAs($this->superAdmin);

    expect(StaffResource::canDelete($other))->toBeTrue();
});
