namespace SatelliteProvider.TestSupport;

public static class IntegrationTestResetGuard
{
    public const string EnvironmentEnvVar = "ASPNETCORE_ENVIRONMENT";
    public const string TestingEnvironment = "Testing";

    public static readonly IReadOnlyList<string> AllowedHosts =
        new[] { "postgres", "localhost", "127.0.0.1" };

    public static void EnsureGuardPassesOrThrow(string? environment, string? host)
    {
        if (!string.Equals(environment, TestingEnvironment, StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException(
                $"Integration test DB reset refused: {EnvironmentEnvVar} is '{environment ?? "(not set)"}', " +
                $"expected '{TestingEnvironment}'. This guard prevents accidental truncate against a non-test " +
                "database (production, staging, or developer-loop DB).");
        }

        if (string.IsNullOrWhiteSpace(host))
        {
            throw new InvalidOperationException(
                "Integration test DB reset refused: connection string has no Host. " +
                "Ensure DB_CONNECTION_STRING is set with Host=postgres (docker-compose) or Host=localhost (developer machine).");
        }

        var hostMatch = AllowedHosts.Any(allowed => string.Equals(allowed, host, StringComparison.OrdinalIgnoreCase));
        if (!hostMatch)
        {
            throw new InvalidOperationException(
                $"Integration test DB reset refused: Host '{host}' is not in the allowed-host list " +
                $"({string.Join(", ", AllowedHosts)}). This guard prevents accidental truncate against a remote " +
                "database whose hostname suggests production or staging.");
        }
    }
}
