<?php

use App\Models\ManagedEnvironmentPermission;
use App\Support\Links\RequiredPermissionsLinks;

it('renders required permissions overview with missing-first ordering and feature cards', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'readonly');

    $configured = config('intune_permissions.permissions', []);
    if (! is_array($configured) || count($configured) < 2) {
        test()->markTestSkipped('Need at least 2 required permissions configured.');
    }

    $grantedKey = (string) ($configured[0]['key'] ?? '');
    $missingKey = (string) ($configured[1]['key'] ?? '');

    if ($grantedKey === '' || $missingKey === '') {
        test()->markTestSkipped('Configured permission keys missing.');
    }

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

    $this->actingAs($user)
        ->get(RequiredPermissionsLinks::requiredPermissions($tenant, ['status' => 'all']))
        ->assertSuccessful()
        ->assertSee('Blocked', false)
        ->assertSeeInOrder([$missingKey, $grantedKey], false);
});
