<?php

declare(strict_types=1);

use App\Filament\Pages\BaselineCompareLanding;
use App\Filament\Pages\BaselineCompareMatrix;
use App\Filament\Pages\Monitoring\Alerts;
use App\Filament\Pages\Monitoring\AuditLog as AuditLogPage;
use App\Filament\Pages\Monitoring\EvidenceOverview;
use App\Filament\Pages\Monitoring\FindingExceptionsQueue;
use App\Filament\Pages\Monitoring\Operations;
use App\Filament\Pages\Operations\TenantlessOperationRunViewer;
use App\Filament\Pages\Reviews\ReviewRegister;
use App\Filament\Pages\EnvironmentDiagnostics;
use App\Filament\Resources\AlertDeliveryResource\Pages\ListAlertDeliveries;
use App\Support\Ui\ActionSurface\ActionSurfaceExemptions;
use App\Support\Ui\ActionSurface\ActionSurfaceValidator;

it('keeps the spec 193 monitoring inventory complete and explicitly classified', function (): void {
    $inventory = ActionSurfaceExemptions::spec193MonitoringSurfaceInventory();

    expect(array_keys($inventory))->toEqualCanonicalizing([
        FindingExceptionsQueue::class,
        TenantlessOperationRunViewer::class,
        Operations::class,
        Alerts::class,
        AuditLogPage::class,
        ListAlertDeliveries::class,
        EvidenceOverview::class,
        BaselineCompareLanding::class,
        BaselineCompareMatrix::class,
        ReviewRegister::class,
        EnvironmentDiagnostics::class,
    ])
        ->and(ActionSurfaceExemptions::spec193MonitoringSurface(FindingExceptionsQueue::class)['classification'] ?? null)->toBe('remediation_required')
        ->and(ActionSurfaceExemptions::spec193MonitoringSurface(FindingExceptionsQueue::class)['surfaceKind'] ?? null)->toBe('queue_workbench')
        ->and(ActionSurfaceExemptions::spec193MonitoringSurface(FindingExceptionsQueue::class)['primaryInspectModel'] ?? null)->toBe('explicit_inspect_action')
        ->and(ActionSurfaceExemptions::spec193MonitoringSurface(Alerts::class)['classification'] ?? null)->toBe('minor_alignment_only')
        ->and(ActionSurfaceExemptions::spec193MonitoringSurface(EvidenceOverview::class)['classification'] ?? null)->toBe('compliant_no_op')
        ->and(ActionSurfaceExemptions::spec193MonitoringSurface(EnvironmentDiagnostics::class)['classification'] ?? null)->toBe('special_type_acceptable');
});

it('keeps tenant diagnostics as the only explicit spec 193 exception surface', function (): void {
    $inventory = ActionSurfaceExemptions::spec193MonitoringSurfaceInventory();

    $exceptionPages = collect($inventory)
        ->filter(fn (array $surface): bool => $surface['classification'] === 'special_type_acceptable')
        ->keys()
        ->values()
        ->all();

    expect($exceptionPages)->toBe([
        EnvironmentDiagnostics::class,
    ])
        ->and(ActionSurfaceExemptions::spec193MonitoringSurface(EnvironmentDiagnostics::class)['exceptionReason'] ?? null)->toContain('diagnostic')
        ->and(ActionSurfaceExemptions::spec193MonitoringSurface(EnvironmentDiagnostics::class)['surfaceKind'] ?? null)->toBe('diagnostic_exception')
        ->and(ActionSurfaceExemptions::spec193MonitoringSurface(EnvironmentDiagnostics::class)['primaryInspectModel'] ?? null)->toBe('singleton_detail_surface')
        ->and(collect($inventory)
            ->except([EnvironmentDiagnostics::class])
            ->every(fn (array $surface): bool => ($surface['exceptionReason'] ?? null) === null))->toBeTrue();
});

it('keeps the spec 193 monitoring inventory valid inside the action-surface validator', function (): void {
    $result = ActionSurfaceValidator::withBaselineExemptions()->validateComponents([]);

    expect($result->hasIssues())->toBeFalse($result->formatForAssertion());
});

it('keeps spec 193 monitoring surfaces out of record-page header layouts', function (): void {
    foreach (array_keys(ActionSurfaceExemptions::spec193MonitoringSurfaceInventory()) as $className) {
        $source = file_get_contents((string) (new \ReflectionClass($className))->getFileName()) ?: '';

        expect($source)
            ->not->toContain('EnterpriseDetail')
            ->not->toContain('enterprise-detail/header');
    }
});
