<?php

use App\Models\TenantPermission;

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', []);

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

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

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

    $missingResponse = $this->actingAs($user)
        ->get("/admin/tenants/{$tenant->external_id}/required-permissions")
        ->assertSuccessful()
        ->assertSee('All required permissions are present', false);

    $missingResponse
        ->assertDontSee('data-permission-key="Alpha.Read.All"', false)
        ->assertDontSee('data-permission-key="Beta.Read.All"', false)
        ->assertDontSee('data-permission-key="Gamma.Manage.All"', false);

    $presentResponse = $this->actingAs($user)
        ->get("/admin/tenants/{$tenant->external_id}/required-permissions?status=present")
        ->assertSuccessful()
        ->assertSee('wire:model.live="status"', false);

    $presentResponse
        ->assertSee('data-permission-key="Alpha.Read.All"', false)
        ->assertSee('data-permission-key="Beta.Read.All"', false)
        ->assertSee('data-permission-key="Gamma.Manage.All"', false);

    $delegatedResponse = $this->actingAs($user)
        ->get("/admin/tenants/{$tenant->external_id}/required-permissions?status=present&type=delegated")
        ->assertSuccessful();

    $delegatedResponse
        ->assertSee('data-permission-key="Beta.Read.All"', false)
        ->assertDontSee('data-permission-key="Alpha.Read.All"', false)
        ->assertDontSee('data-permission-key="Gamma.Manage.All"', false);

    $featureQuery = http_build_query([
        'status' => 'all',
        'features' => ['backup'],
    ]);

    $featureResponse = $this->actingAs($user)
        ->get("/admin/tenants/{$tenant->external_id}/required-permissions?{$featureQuery}")
        ->assertSuccessful();

    $featureResponse
        ->assertSee('data-permission-key="Alpha.Read.All"', false)
        ->assertSee('data-permission-key="Gamma.Manage.All"', false)
        ->assertDontSee('data-permission-key="Beta.Read.All"', false);

    $searchResponse = $this->actingAs($user)
        ->get("/admin/tenants/{$tenant->external_id}/required-permissions?status=all&search=delegated")
        ->assertSuccessful();

    $searchResponse
        ->assertSee('data-permission-key="Beta.Read.All"', false)
        ->assertDontSee('data-permission-key="Alpha.Read.All"', false)
        ->assertDontSee('data-permission-key="Gamma.Manage.All"', false);
});
