namespace MBN_STOCK_WEBVIEW.LegacyWeb.Tests;

public sealed class LegacyDbSmokeRuntimeSelectionContractTests
{
    private static readonly string RepositoryRoot = FindRepositoryRoot();
    private static readonly string Program = File.ReadAllText(Path.Combine(
        RepositoryRoot,
        "tools",
        "MBN_STOCK_WEBVIEW.DbSmoke",
        "Program.cs"));
    private static readonly string NxtAudit = File.ReadAllText(Path.Combine(
        RepositoryRoot,
        "tools",
        "MBN_STOCK_WEBVIEW.DbSmoke",
        "NxtThemeRestoreDbAudit.cs"));

    [Fact]
    public void SelectedLegacyRuntimeEndpointIsInjectedIntoNxtAudit()
    {
        Assert.Contains(
            "var iniPath = Path.Combine(root, \"Res\", \"MmoneyCoder.ini\");",
            Program,
            StringComparison.Ordinal);
        Assert.Contains(
            "new LegacyIniDatabaseOptionsLoader(_ => null).Load(iniPath)",
            Program,
            StringComparison.Ordinal);
        Assert.Contains(
            "MariaDbEndpointReference.From(selectedOptions.MariaDb)",
            Program,
            StringComparison.Ordinal);
        Assert.Contains(
            "selection.ReferenceMariaDb",
            Program,
            StringComparison.Ordinal);
        Assert.Contains(
            "MariaDbEndpointReference referenceOptions",
            NxtAudit,
            StringComparison.Ordinal);
        Assert.Contains(
            "referenceOptions.Host",
            NxtAudit,
            StringComparison.Ordinal);
        Assert.Contains(
            "referenceOptions.Port",
            NxtAudit,
            StringComparison.Ordinal);
        Assert.Contains(
            "referenceOptions.Database",
            NxtAudit,
            StringComparison.Ordinal);
    }

    [Fact]
    public void JsonRuntimeEndpointIsCapturedBeforeEnvironmentOverrides()
    {
        Assert.Contains(
            "new DatabaseOptionsLoader(_ => null).Load(configurationPath)",
            Program,
            StringComparison.Ordinal);
        Assert.Contains(
            "var runtime = DatabaseRuntime.CreateDefault(configurationPath);",
            Program,
            StringComparison.Ordinal);
        Assert.Contains(
            "HasCompleteEndpointIdentity(configuredMariaDb)",
            Program,
            StringComparison.Ordinal);
        Assert.Contains(
            "MariaDbEndpointReference.From(configuredMariaDb)",
            Program,
            StringComparison.Ordinal);
        Assert.Contains(
            "MariaDbEndpointReference.From(runtime.Options.MariaDb)",
            Program,
            StringComparison.Ordinal);
    }

    [Fact]
    public void NxtAuditHasNoMachineSpecificOriginalConfigurationDependency()
    {
        Assert.DoesNotContain("OriginalConfigurationPath", NxtAudit, StringComparison.Ordinal);
        Assert.DoesNotContain("ReadOriginalEndpoint", NxtAudit, StringComparison.Ordinal);
        Assert.DoesNotContain("MBN_STOCK_N", NxtAudit, StringComparison.OrdinalIgnoreCase);
        Assert.DoesNotContain("MmoneyCoder.ini", NxtAudit, StringComparison.OrdinalIgnoreCase);
    }

    [Fact]
    public void InjectedReferenceContainsEndpointIdentityOnly()
    {
        Assert.Contains(
            "internal sealed record MariaDbEndpointReference(",
            NxtAudit,
            StringComparison.Ordinal);
        Assert.Contains("string? Host", NxtAudit, StringComparison.Ordinal);
        Assert.Contains("int Port", NxtAudit, StringComparison.Ordinal);
        Assert.Contains("string? Database", NxtAudit, StringComparison.Ordinal);
        Assert.DoesNotContain("UserName", NxtAudit, StringComparison.Ordinal);
        Assert.DoesNotContain("Password", NxtAudit, StringComparison.Ordinal);

        Assert.Contains("originalSha256=", NxtAudit, StringComparison.Ordinal);
        Assert.Contains("currentSha256=", NxtAudit, StringComparison.Ordinal);
        Assert.Contains("configuredEqual=", NxtAudit, StringComparison.Ordinal);
    }

    private static string FindRepositoryRoot()
    {
        var current = new DirectoryInfo(AppContext.BaseDirectory);
        while (current is not null)
        {
            if (File.Exists(Path.Combine(current.FullName, "MBN_STOCK_WEBVIEW.sln")))
            {
                return current.FullName;
            }

            current = current.Parent;
        }

        throw new DirectoryNotFoundException("Could not locate the repository root.");
    }
}
