namespace MBN_STOCK_WEBVIEW.LegacyWeb.Tests;

public sealed class LegacyRuntimeStockCutMenuNativeContractTests
{
    private static readonly string RepositoryRoot = FindRepositoryRoot();
    private static readonly string MainWindow = File.ReadAllText(Path.Combine(
        RepositoryRoot,
        "src",
        "MBN_STOCK_WEBVIEW.LegacyParityApp",
        "MainWindow.xaml.cs"));

    [Fact]
    public void MainWindowUsesExternalThenPackagedCandidatesAndInjectsResolvedRows()
    {
        Assert.Contains(
            "Environment.SpecialFolder.LocalApplicationData",
            MainWindow,
            StringComparison.Ordinal);
        Assert.Contains("\"MBN_STOCK_WEBVIEW\"", MainWindow, StringComparison.Ordinal);
        Assert.Contains("\"Res\"", MainWindow, StringComparison.Ordinal);
        Assert.Contains("\"종목.ini\"", MainWindow, StringComparison.Ordinal);
        Assert.Contains(
            "LegacyRuntimeStockCutMenuLoader.ResolveFromCandidates(",
            MainWindow,
            StringComparison.Ordinal);
        Assert.Contains("runtimeCutMenuOverridePath", MainWindow,
            StringComparison.Ordinal);
        Assert.Contains("AppContext.BaseDirectory", MainWindow, StringComparison.Ordinal);
        Assert.Contains("cutRows: cutMenuComposition.Rows", MainWindow,
            StringComparison.Ordinal);
        Assert.DoesNotContain("Environment.CurrentDirectory", MainWindow,
            StringComparison.Ordinal);
        Assert.DoesNotContain("Directory.GetCurrentDirectory", MainWindow,
            StringComparison.Ordinal);
        Assert.DoesNotContain("MBN_STOCK_N", MainWindow, StringComparison.OrdinalIgnoreCase);
        Assert.DoesNotContain("bin\\Debug\\Res\\종목.ini", MainWindow,
            StringComparison.OrdinalIgnoreCase);
    }

    [Fact]
    public void DatabaseInitializationErrorWinsOverCombinedRuntimeCatalogWarnings()
    {
        var errorBranch = MainWindow.IndexOf(
            "if (initializationError is not null)",
            StringComparison.Ordinal);
        var warningBranch = MainWindow.IndexOf(
            "cutMenuComposition.WarningMessage,",
            errorBranch,
            StringComparison.Ordinal);
        var runtimeUiWarning = MainWindow.IndexOf(
            "runtimeUiCatalogs.WarningMessage",
            warningBranch,
            StringComparison.Ordinal);
        var errorReport = MainWindow.IndexOf(
            "_controller.ReportError(initializationError);",
            errorBranch,
            StringComparison.Ordinal);
        var warningReport = MainWindow.IndexOf(
            "_controller.ReportWarning(string.Join(\" \", catalogWarnings));",
            warningBranch,
            StringComparison.Ordinal);

        Assert.True(errorBranch >= 0);
        Assert.True(errorReport > errorBranch);
        Assert.True(warningBranch > errorReport);
        Assert.True(runtimeUiWarning > warningBranch);
        Assert.True(warningReport > warningBranch);
        Assert.Equal(
            warningReport,
            MainWindow.LastIndexOf(
                "_controller.ReportWarning(string.Join(\" \", catalogWarnings));",
                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.");
    }
}
