<?php

declare(strict_types=1);

it('keeps targeted provider-state runtime paths free of legacy provider status fallbacks', function (): void {
    $guardedFiles = [
        'apps/platform/app/Models/ProviderConnection.php',
        'apps/platform/app/Services/Providers/Contracts/HealthResult.php',
        'apps/platform/app/Services/Providers/MicrosoftProviderHealthCheck.php',
        'apps/platform/app/Services/Providers/ProviderConnectionMutationService.php',
        'apps/platform/app/Services/Providers/ProviderConnectionResolver.php',
        'apps/platform/app/Services/Providers/ProviderConnectionStateProjector.php',
        'apps/platform/app/Jobs/ProviderConnectionHealthCheckJob.php',
        'apps/platform/app/Filament/Resources/ProviderConnectionResource.php',
        'apps/platform/app/Filament/Resources/ManagedEnvironmentResource.php',
        'apps/platform/app/Filament/System/Pages/Directory/Tenants.php',
        'apps/platform/app/Filament/System/Pages/Directory/ViewTenant.php',
        'apps/platform/resources/views/filament/infolists/entries/provider-connection-state.blade.php',
        'apps/platform/resources/views/filament/system/pages/directory/view-tenant.blade.php',
    ];

    $forbiddenNeedles = [
        'health_status',
        'BadgeDomain::ProviderConnectionStatus',
        'BadgeDomain::ProviderConnectionHealth',
        'ProviderConnectionStatusBadge',
        'ProviderConnectionHealthBadge',
        "'status' => 'connected'",
        "'status' => 'needs_consent'",
        "'status' => 'disabled'",
        "'status' => 'error'",
    ];

    $violations = [];

    foreach ($guardedFiles as $relativePath) {
        $contents = file_get_contents(repo_path($relativePath));

        expect($contents)->toBeString();

        foreach ($forbiddenNeedles as $needle) {
            if (! str_contains($contents, $needle)) {
                continue;
            }

            $violations[] = sprintf('%s contains forbidden legacy provider-state marker "%s".', $relativePath, $needle);
        }
    }

    expect($violations)->toBe([]);
});
