<?php

declare(strict_types=1);

use App\Support\Ui\GovernanceActions\GovernanceActionCatalog;

it('keeps the spec 194 family inventory, surface bindings, and documented deviations explicit', function (): void {
    $families = GovernanceActionCatalog::families();
    $rules = GovernanceActionCatalog::rules();
    $bindings = GovernanceActionCatalog::surfaceBindings();

    expect(array_keys($families))->toEqualCanonicalizing([
        'exception_decision',
        'review_lifecycle',
        'evidence_lifecycle',
        'run_triage',
        'finding_lifecycle',
        'tenant_lifecycle',
    ])
        ->and(array_keys($rules))->toHaveCount(17)
        ->and($bindings)->not->toBeEmpty();

    foreach ($bindings as $binding) {
        $matchingRule = collect($rules)->first(
            fn ($rule): bool => $rule->familyKey === $binding['familyKey']
                && in_array($binding['surfaceKey'], $rule->surfaceKeys, true),
        );

        expect($matchingRule)->not->toBeNull();
    }

    expect(GovernanceActionCatalog::documentedDeviations())->not->toBeEmpty();
});

it('keeps evidence and review surface bindings aligned to their canonical action names', function (): void {
    $bindingsBySurface = collect(GovernanceActionCatalog::surfaceBindings())->groupBy('surfaceKey');

    expect($bindingsBySurface->get('view_evidence_snapshot', collect())->pluck('actionName')->all())
        ->toEqualCanonicalizing(['refresh_evidence', 'expire_snapshot'])
        ->and($bindingsBySurface->get('view_environment_review', collect())->pluck('actionName')->all())
        ->toContain('refresh_review', 'publish_review', 'archive_review');
});

it('keeps triage mutations out of the tenantless run viewer while the system run page owns them', function (): void {
    $tenantlessViewer = file_get_contents(base_path('app/Filament/Pages/Operations/TenantlessOperationRunViewer.php'));
    $systemViewRun = file_get_contents(base_path('app/Filament/System/Pages/Ops/ViewRun.php'));

    expect($tenantlessViewer)->toBeString()
        ->and($systemViewRun)->toBeString()
        ->and($tenantlessViewer)->not->toContain("Action::make('retry')")
        ->and($tenantlessViewer)->not->toContain("Action::make('cancel')")
        ->and($tenantlessViewer)->not->toContain("Action::make('mark_investigated')")
        ->and($systemViewRun)->toContain("Action::make('retry')")
        ->and($systemViewRun)->toContain("Action::make('cancel')")
        ->and($systemViewRun)->toContain("Action::make('mark_investigated')");
});

it('keeps the governed surface files inside the catalog binding inventory', function (): void {
    $boundFiles = collect(GovernanceActionCatalog::surfaceBindings())
        ->pluck('pageClass')
        ->unique()
        ->values()
        ->all();

    expect($boundFiles)->toContain(
        'App\\Filament\\Pages\\Monitoring\\FindingExceptionsQueue',
        'App\\Filament\\Resources\\FindingExceptionResource\\Pages\\ViewFindingException',
        'App\\Filament\\Resources\\EvidenceSnapshotResource\\Pages\\ViewEvidenceSnapshot',
        'App\\Filament\\Resources\\EnvironmentReviewResource\\Pages\\ViewEnvironmentReview',
        'App\\Filament\\System\\Pages\\Ops\\ViewRun',
        'App\\Filament\\Resources\\FindingResource\\Pages\\ViewFinding',
        'App\\Filament\\Resources\\ManagedEnvironmentResource\\Pages\\ViewManagedEnvironment',
        'App\\Filament\\Resources\\ManagedEnvironmentResource\\Pages\\EditManagedEnvironment',
    );
});
