namespace MBN_STOCK_WEBVIEW.LegacyWeb.Tests;

public sealed class LegacyOverseasAllNationPlayoutContractTests
{
    [Fact]
    public void NativeRuntimeKeepsUc5ExactIdentityForOneColumnAndCandleRoutes()
    {
        var root = FindRepositoryRoot();
        var runtime = File.ReadAllText(Path.Combine(root, "LegacySceneRuntimeFactory.cs"));
        var resolver = File.ReadAllText(Path.Combine(
            root,
            "src",
            "MBN_STOCK_WEBVIEW.Core",
            "Playout",
            "Scenes",
            "LegacyParameterizedSceneRequestResolver.cs"));

        Assert.Contains("parameterizedRequestResolver.CreateS5001Request(entry)", runtime,
            StringComparison.Ordinal);
        Assert.Contains("[\"5001\", \"N5001\"]", runtime, StringComparison.Ordinal);
        Assert.Contains("[\"8035\", \"8061\", \"8040\", \"8046\", \"8051\", \"8056\"]", runtime,
            StringComparison.Ordinal);
        Assert.Contains("ParseForeignCandleIdentity(selection.DataCode, target)", runtime,
            StringComparison.Ordinal);
        Assert.Contains("LegacyNamedForeignStockRestoreService.IsCanonicalBoundSymbol(parts[0])", runtime,
            StringComparison.Ordinal);
        Assert.DoesNotContain("parts[1] is not (\"US\" or \"TW\")", runtime,
            StringComparison.Ordinal);
        Assert.Contains("new S5001ExactForeignStockLoadRequest(", resolver,
            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("Repository root was not found.");
    }
}
