namespace MBN_STOCK_WEBVIEW.LegacyWeb.Tests;

public sealed class LegacyNativeDialogParityWebTests
{
    private static readonly string RepositoryRoot = FindRepositoryRoot();
    private static readonly string App = File.ReadAllText(Path.Combine(
        RepositoryRoot,
        "src",
        "MBN_STOCK_WEBVIEW.LegacyParityApp",
        "Web",
        "app.js"));
    private static readonly string Controller = File.ReadAllText(Path.Combine(
        RepositoryRoot,
        "src",
        "MBN_STOCK_WEBVIEW.LegacyApplication",
        "LegacyOperatorController.cs"));
    private static readonly string PackageInputHarness = File.ReadAllText(Path.Combine(
        RepositoryRoot,
        "scripts",
        "Test-LegacyPackageInput.mjs"));

    [Fact]
    public void Original_destructive_questions_are_authoritative_MmoneyCoder_dialogs()
    {
        foreach (var intent in new[]
                 {
                     "delete-all-playlist-rows",
                     "delete-manual-financial",
                     "delete-all-manual-financial",
                     "delete-all-manual-vi-items"
                 })
        {
            Assert.Contains($"send(\"{intent}\", {{}})", App, StringComparison.Ordinal);
        }

        Assert.DoesNotContain(
            "window.confirm(\"송출 리스트를 모두 삭제하겠습니까?\")",
            App,
            StringComparison.Ordinal);
        Assert.DoesNotContain(
            "window.confirm(\"선택한 수동 재무 데이터를 삭제하시겠습니까?\")",
            App,
            StringComparison.Ordinal);
        Assert.DoesNotContain(
            "window.confirm(\"현재 화면의 수동 재무 데이터를 모두 삭제하시겠습니까?\")",
            App,
            StringComparison.Ordinal);
        Assert.DoesNotContain(
            "window.confirm(\"VI 목록을 모두 지우시겠습니까?\")",
            App,
            StringComparison.Ordinal);
        Assert.DoesNotContain(
            "clear.disabled = manual.viItems.length === 0",
            App,
            StringComparison.Ordinal);

        foreach (var message in new[]
                 {
                     "송출 리스트를 모두 삭제하겠습니까?",
                     "선택한 종목을 삭제하겠습니까?",
                     "모든 종목을 삭제하겠습니까?",
                     "모두 삭제하겠습니까?"
                 })
        {
            Assert.Contains(message, Controller, StringComparison.Ordinal);
        }

        Assert.Contains("IsConfirmation: true", Controller, StringComparison.Ordinal);
        Assert.Contains("PendingNativeConfirmation.ForPlaylistDeleteAll", Controller,
            StringComparison.Ordinal);
        Assert.Contains("PendingNativeConfirmation.ForManualFinancialDelete", Controller,
            StringComparison.Ordinal);
        Assert.Contains("PendingNativeConfirmation.ForManualViDeleteAll", Controller,
            StringComparison.Ordinal);
    }

    [Fact]
    public void PList_confirm_save_requests_state_driven_saved_dialog_without_browser_alert()
    {
        Assert.DoesNotContain(
            "window.alert(\"저장되었습니다\")",
            App,
            StringComparison.Ordinal);
        Assert.Contains(
            "if (command === \"save-current-named-playlist-to\")",
            App,
            StringComparison.Ordinal);
        Assert.Contains("payload.showSavedAlert = showSavedAlert === true", App,
            StringComparison.Ordinal);
        Assert.Contains("if (succeeded && showSavedAlert)", Controller,
            StringComparison.Ordinal);
        Assert.Contains("new LegacyDialogState(\"저장되었습니다\")", Controller,
            StringComparison.Ordinal);
    }

    [Fact]
    public void Package_input_smoke_answers_the_native_dialogs_it_now_exercises()
    {
        foreach (var label in new[]
                 {
                     "PList confirm row save",
                     "PROGRAM confirm named-playlist row save",
                     "GraphE ${screen} save",
                     "GraphE ${screen} delete",
                     "GraphE ${screen} delete-all cancel branch",
                     "VIList delete-all cancel branch",
                     "screen validation playlist cleanup"
                 })
        {
            Assert.Contains(label, PackageInputHarness, StringComparison.Ordinal);
        }

        Assert.Equal(
            8,
            PackageInputHarness.Split(
                "pointerClickWithNativeDialog(",
                StringSplitOptions.None).Length - 1);
        // The only remaining browser confirm is the separate one-time import
        // prompt, which has not been migrated to LegacyDialogState.
        Assert.Equal(
            2,
            PackageInputHarness.Split(
                "pointerClickWithJavaScriptDialog(",
                StringSplitOptions.None).Length - 1);
    }

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

            directory = directory.Parent;
        }

        throw new DirectoryNotFoundException("Repository root was not found.");
    }
}
