namespace MBN_STOCK_WEBVIEW.LegacyWeb.Tests;

public sealed class LegacyNamedPlaylistDispatchFailureContractTests
{
    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 NativeRejectionsAndUnexpectedExceptionsTerminateCorrelatedCommands()
    {
        Assert.Contains(
            "PostState(IsNamedPlaylistModalIntent(intent)",
            MainWindow,
            StringComparison.Ordinal);
        Assert.Contains(
            "var failureState = ReportIntentFailure(",
            MainWindow,
            StringComparison.Ordinal);
        Assert.Contains(
            "ObserveOperatorMutationQuarantine(failureState);",
            MainWindow,
            StringComparison.Ordinal);
        Assert.Contains(
            "writeOutcomeMayBeUnknown: dispatchStarted",
            MainWindow,
            StringComparison.Ordinal);
        Assert.Contains(
            "_controller.ReportNamedPlaylistWriteOutcomeUnknown(",
            MainWindow,
            StringComparison.Ordinal);
        Assert.Contains("RefreshNamedPlaylistsForDispatchAsync(", MainWindow,
            StringComparison.Ordinal);
        Assert.Contains("SelectNamedPlaylistForDispatch(", MainWindow,
            StringComparison.Ordinal);
        Assert.Contains("CreateNamedPlaylistForDispatchAsync(", MainWindow,
            StringComparison.Ordinal);
        Assert.Contains("DeleteNamedPlaylistForDispatchAsync(", MainWindow,
            StringComparison.Ordinal);
        Assert.Contains("_controller.CompleteNamedPlaylistDispatch(", MainWindow,
            StringComparison.Ordinal);
        Assert.Contains(
            "_controller.ReportNamedPlaylistCommandFailure(\n" +
            "                    \"load-named-playlist-by-id\"",
            MainWindow.Replace("\r\n", "\n", StringComparison.Ordinal),
            StringComparison.Ordinal);
        Assert.Contains(
            "_controller.ReportNamedPlaylistCommandFailure(\n" +
            "                    \"save-current-named-playlist-to\"",
            MainWindow.Replace("\r\n", "\n", StringComparison.Ordinal),
            StringComparison.Ordinal);
    }

    [Fact]
    public void SelectionUsesAnExactReceiptWithoutDisablingTheSecondDoubleClick()
    {
        var normalized = MainWindow.Replace("\r\n", "\n", StringComparison.Ordinal);

        Assert.Contains(
            "LegacySelectNamedPlaylistIntent selection =>\n" +
            "                SelectNamedPlaylistForDispatch(",
            normalized,
            StringComparison.Ordinal);
        Assert.Contains(
            "intent is LegacyRefreshNamedPlaylistsIntent or\n" +
            "            LegacySelectNamedPlaylistIntent or",
            normalized,
            StringComparison.Ordinal);
        Assert.DoesNotContain(
            "LegacyRefreshNamedPlaylistsIntent or\n" +
            "                    LegacySelectNamedPlaylistIntent or",
            normalized,
            StringComparison.Ordinal);
        Assert.Contains(
            "\"select-named-playlist\",\n" +
            "            requestId,\n" +
            "            succeeded",
            normalized,
            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.");
    }
}
