<?php

declare(strict_types=1);

use App\Filament\Resources\BaselineProfileResource;
use App\Filament\Resources\EvidenceSnapshotResource;
use App\Filament\Resources\FindingExceptionResource;
use App\Filament\Resources\ManagedEnvironmentResource;
use App\Filament\Resources\EnvironmentReviewResource;
use App\Filament\Resources\ManagedEnvironmentResource\Pages\EditManagedEnvironment;
use App\Filament\Resources\ManagedEnvironmentResource\Pages\ViewManagedEnvironment;
use App\Support\Ui\ActionSurface\ActionSurfaceExemptions;
use App\Support\Ui\ActionSurface\ActionSurfaceValidator;

function spec192RecordPageSource(string $className): string
{
    $reflection = new ReflectionClass($className);
    $path = $reflection->getFileName();

    expect($path)->toBeString();

    return file_get_contents((string) $path) ?: '';
}

it('keeps the spec 192 record-page inventory complete and explicitly classified', function (): void {
    $inventory = ActionSurfaceExemptions::spec192RecordPageInventory();

    expect(array_keys($inventory))->toEqualCanonicalizing([
        \App\Filament\Resources\BaselineProfileResource\Pages\ViewBaselineProfile::class,
        \App\Filament\Resources\EvidenceSnapshotResource\Pages\ViewEvidenceSnapshot::class,
        \App\Filament\Resources\FindingExceptionResource\Pages\ViewFindingException::class,
        \App\Filament\Resources\EnvironmentReviewResource\Pages\ViewEnvironmentReview::class,
        EditManagedEnvironment::class,
        ViewManagedEnvironment::class,
        \App\Filament\Resources\ProviderConnectionResource\Pages\ViewProviderConnection::class,
        \App\Filament\Resources\FindingResource\Pages\ViewFinding::class,
        \App\Filament\Resources\ReviewPackResource\Pages\ViewReviewPack::class,
        \App\Filament\Resources\AlertDestinationResource\Pages\ViewAlertDestination::class,
        \App\Filament\Resources\PolicyVersionResource\Pages\ViewPolicyVersion::class,
        \App\Filament\Resources\Workspaces\Pages\ViewWorkspace::class,
        \App\Filament\Resources\BaselineSnapshotResource\Pages\ViewBaselineSnapshot::class,
        \App\Filament\Resources\BackupSetResource\Pages\ViewBackupSet::class,
    ]);

    expect(ActionSurfaceExemptions::spec192RecordPageSurface(ViewManagedEnvironment::class))
        ->not->toBeNull()
        ->and(ActionSurfaceExemptions::spec192RecordPageSurface(ViewManagedEnvironment::class)['classification'] ?? null)->toBe('workflow_heavy_special_type')
        ->and(ActionSurfaceExemptions::spec192RecordPageSurface(ViewManagedEnvironment::class)['exceptionReason'] ?? null)->toContain('workflow-heavy hub')
        ->and(ActionSurfaceExemptions::spec192RecordPageSurface(EditManagedEnvironment::class)['classification'] ?? null)->toBe('remediation_required')
        ->and(ActionSurfaceExemptions::spec192RecordPageSurface(EditManagedEnvironment::class)['allowsNoPrimaryAction'] ?? null)->toBeTrue();
});

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

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

it('keeps remediated spec 192 pages off the enterprise-detail body-layout builder', function (): void {
    foreach ([
        BaselineProfileResource::class,
        EvidenceSnapshotResource::class,
        FindingExceptionResource::class,
        EnvironmentReviewResource::class,
        ManagedEnvironmentResource::class,
        EditManagedEnvironment::class,
        ViewManagedEnvironment::class,
    ] as $className) {
        expect(spec192RecordPageSource($className))
            ->not->toContain('EnterpriseDetail')
            ->not->toContain('enterprise-detail/header');
    }
});
