<?php

declare(strict_types=1);

it('keeps cutover-owned route emission seams free of retired management paths', function (): void {
    $root = base_path();

    $forbiddenPatternsByFile = [
        'routes/web.php' => [
            '/\/admin\/tenants\/[^\'"\n]*\/provider-connections(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/provider-connections(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/required-permissions(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/memberships(?:[\/?"\']|$)/',
            '/\/admin\/t\/t\//',
        ],
        'app/Providers/Filament/AdminPanelProvider.php' => [
            '/panel:\s*[\'\"]tenant[\'\"]/',
            '/\/admin\/tenants\/[^\'"\n]*\/provider-connections(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/provider-connections(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/required-permissions(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/memberships(?:[\/?"\']|$)/',
            '/\/admin\/t\/t\//',
        ],
        'app/Filament/Resources/ManagedEnvironmentResource.php' => [
            '/panel:\s*[\'\"]tenant[\'\"]/',
            '/\/admin\/tenants\/[^\'"\n]*\/provider-connections(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/provider-connections(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/required-permissions(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/memberships(?:[\/?"\']|$)/',
            '/\/admin\/t\/t\//',
        ],
        'app/Support/OperationRunLinks.php' => [
            '/panel:\s*[\'\"]tenant[\'\"]/',
            '/\/admin\/tenants\/[^\'"\n]*\/provider-connections(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/provider-connections(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/required-permissions(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/memberships(?:[\/?"\']|$)/',
            '/\/admin\/t\/t\//',
        ],
        'app/Support/Verification/VerificationLinkBehavior.php' => [
            '/panel:\s*[\'\"]tenant[\'\"]/',
            '/\/admin\/tenants\/[^\'"\n]*\/provider-connections(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/provider-connections(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/required-permissions(?:[\/?"\']|$)/',
            '/\/admin\/t\/[^\'"\n]*\/memberships(?:[\/?"\']|$)/',
            '/\/admin\/t\/t\//',
        ],
    ];

    $hits = [];

    foreach ($forbiddenPatternsByFile as $relativePath => $patterns) {
        $absolutePath = $root.'/'.$relativePath;

        expect(is_file($absolutePath))->toBeTrue("Expected guard-owned seam [{$relativePath}] to exist.");

        $contents = file_get_contents($absolutePath);

        if (! is_string($contents) || $contents === '') {
            continue;
        }

        $lines = preg_split('/\R/', $contents) ?: [];

        foreach ($patterns as $pattern) {
            foreach ($lines as $index => $line) {
                if (preg_match($pattern, $line) !== 1) {
                    continue;
                }

                $hits[] = $relativePath.':'.($index + 1).' -> '.trim($line);
            }
        }
    }

    expect($hits)->toBeEmpty("Retired management path or tenant-panel route emission detected on a Spec 288 seam:\n".implode("\n", $hits));
});

it('keeps spec 288 proof seams free of reintroduced retired tenant-panel helpers', function (): void {
    $root = base_path();

    $forbiddenPatternsByFile = [
        'tests/Pest.php' => [
            '/Filament::setCurrentPanel\(\s*[\'\"]tenant[\'\"]\s*\)/',
            '/Filament::getPanel\(\s*[\'\"]tenant[\'\"]\s*\)/',
        ],
        'tests/Browser/Spec281ProviderConnectionScopeSmokeTest.php' => [
            '/setTenantPanelContext\s*\(/',
            '/panel:\s*[\'\"]tenant[\'\"]/',
            '/Filament::setCurrentPanel\(\s*[\'\"]tenant[\'\"]\s*\)/',
            '/Filament::getPanel\(\s*[\'\"]tenant[\'\"]\s*\)/',
        ],
        'tests/Browser/Spec285WorkspaceRbacEnvironmentAccessSmokeTest.php' => [
            '/setTenantPanelContext\s*\(/',
            '/panel:\s*[\'\"]tenant[\'\"]/',
            '/Filament::setCurrentPanel\(\s*[\'\"]tenant[\'\"]\s*\)/',
            '/Filament::getPanel\(\s*[\'\"]tenant[\'\"]\s*\)/',
        ],
        'tests/Feature/ProviderConnections/LegacyRedirectTest.php' => [
            '/setTenantPanelContext\s*\(/',
            '/panel:\s*[\'\"]tenant[\'\"]/',
        ],
        'tests/Feature/ManagedEnvironment/LegacyTenantCoreGuardTest.php' => [
            '/setTenantPanelContext\s*\(/',
            '/panel:\s*[\'\"]tenant[\'\"]/',
        ],
        'tests/Feature/Spec080WorkspaceManagedTenantAdminMigrationTest.php' => [
            '/setTenantPanelContext\s*\(/',
            '/panel:\s*[\'\"]tenant[\'\"]/',
        ],
    ];

    $hits = [];

    foreach ($forbiddenPatternsByFile as $relativePath => $patterns) {
        $absolutePath = $root.'/'.$relativePath;

        expect(is_file($absolutePath))->toBeTrue("Expected Spec 288 proof seam [{$relativePath}] to exist.");

        $contents = file_get_contents($absolutePath);

        if (! is_string($contents) || $contents === '') {
            continue;
        }

        $lines = preg_split('/\R/', $contents) ?: [];

        foreach ($patterns as $pattern) {
            foreach ($lines as $index => $line) {
                if (preg_match($pattern, $line) !== 1) {
                    continue;
                }

                $hits[] = $relativePath.':'.($index + 1).' -> '.trim($line);
            }
        }
    }

    expect($hits)->toBeEmpty("Forbidden retired tenant-panel helper or bootstrapping detected on a Spec 288 proof seam; setTenantPanelContext() must not be reintroduced as a current helper:\n".implode("\n", $hits));
});
