<?php

declare(strict_types=1);

use App\Filament\Pages\EnvironmentRequiredPermissions;
use App\Models\ManagedEnvironmentPermission;
use App\Support\Workspaces\WorkspaceContext;
use Livewire\Livewire;

it('narrows required permissions results using filters and search', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'readonly');

    config()->set('intune_permissions.permissions', [
        [
            'key' => 'Alpha.Read.All',
            'type' => 'application',
            'description' => 'Alpha application permission',
            'features' => ['backup'],
        ],
        [
            'key' => 'Beta.Read.All',
            'type' => 'delegated',
            'description' => 'Beta delegated permission',
            'features' => ['restore'],
        ],
        [
            'key' => 'Gamma.Manage.All',
            'type' => 'application',
            'description' => 'Gamma restore permission',
            'features' => ['backup', 'restore'],
        ],
    ]);
    config()->set('entra_permissions.permissions', []);

    ManagedEnvironmentPermission::create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'permission_key' => 'Alpha.Read.All',
        'status' => 'granted',
        'details' => ['source' => 'db'],
        'last_checked_at' => now(),
    ]);

    ManagedEnvironmentPermission::create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'permission_key' => 'Beta.Read.All',
        'status' => 'granted',
        'details' => ['source' => 'db'],
        'last_checked_at' => now(),
    ]);

    ManagedEnvironmentPermission::create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'permission_key' => 'Gamma.Manage.All',
        'status' => 'granted',
        'details' => ['source' => 'db'],
        'last_checked_at' => now(),
    ]);

    $this->actingAs($user);
    setAdminPanelContext();
    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);

    Livewire::withQueryParams([
        'tenant' => (string) $tenant->external_id,
    ])
        ->test(EnvironmentRequiredPermissions::class)
        ->assertSet('tableFilters.status.value', 'missing')
        ->assertSee('All required permissions are present')
        ->assertCanNotSeeTableRecords([
            'Alpha.Read.All',
            'Beta.Read.All',
            'Gamma.Manage.All',
        ]);

    Livewire::withQueryParams([
        'tenant' => (string) $tenant->external_id,
        'status' => 'present',
    ])
        ->test(EnvironmentRequiredPermissions::class)
        ->assertSet('tableFilters.status.value', 'present')
        ->assertCanSeeTableRecords([
            'Alpha.Read.All',
            'Beta.Read.All',
            'Gamma.Manage.All',
        ]);

    Livewire::withQueryParams([
        'tenant' => (string) $tenant->external_id,
        'status' => 'present',
        'type' => 'delegated',
    ])
        ->test(EnvironmentRequiredPermissions::class)
        ->assertSet('tableFilters.status.value', 'present')
        ->assertSet('tableFilters.type.value', 'delegated')
        ->assertCanSeeTableRecords(['Beta.Read.All'])
        ->assertCanNotSeeTableRecords([
            'Alpha.Read.All',
            'Gamma.Manage.All',
        ]);

    Livewire::withQueryParams([
        'tenant' => (string) $tenant->external_id,
        'status' => 'all',
        'features' => ['backup'],
    ])
        ->test(EnvironmentRequiredPermissions::class)
        ->assertSet('tableFilters.features.values', ['backup'])
        ->assertCanSeeTableRecords([
            'Alpha.Read.All',
            'Gamma.Manage.All',
        ])
        ->assertCanNotSeeTableRecords(['Beta.Read.All']);

    Livewire::withQueryParams([
        'tenant' => (string) $tenant->external_id,
        'status' => 'all',
        'search' => 'delegated',
    ])
        ->test(EnvironmentRequiredPermissions::class)
        ->assertSet('tableSearch', 'delegated')
        ->assertCanSeeTableRecords(['Beta.Read.All'])
        ->assertCanNotSeeTableRecords([
            'Alpha.Read.All',
            'Gamma.Manage.All',
        ]);
});
