namespace GmRelay.Bot.Tests.Web;

public sealed class MiniAppDashboardTests
{
    [Fact]
    public async Task MiniAppPage_ShouldExposeTelegramWebAppDashboardEntryPoint()
    {
        var miniAppPage = await File.ReadAllTextAsync(FindRepositoryFile("src/GmRelay.Web/Components/Pages/MiniApp.razor"));

        Assert.Contains("@page \"/miniapp\"", miniAppPage, StringComparison.Ordinal);
        Assert.Contains("authenticateTelegramMiniApp", miniAppPage, StringComparison.Ordinal);
        Assert.Contains("/auth/telegram-webapp", miniAppPage, StringComparison.Ordinal);
        Assert.Contains("watchTelegramMiniAppLogin", miniAppPage, StringComparison.Ordinal);
        Assert.Contains("/auth/status", miniAppPage, StringComparison.Ordinal);
        Assert.Contains("miniAppAuthStatus", miniAppPage, StringComparison.Ordinal);
        Assert.Contains("telegram-webapp-missing", miniAppPage, StringComparison.Ordinal);
        Assert.Contains("telegram-init-data-empty", miniAppPage, StringComparison.Ordinal);
        Assert.Contains("telegram-auth-failed", miniAppPage, StringComparison.Ordinal);
    }

    [Fact]
    public async Task AppShell_ShouldLoadTelegramWebAppScriptAndAuthenticator()
    {
        var appShell = await File.ReadAllTextAsync(FindRepositoryFile("src/GmRelay.Web/Components/App.razor"));

        Assert.Contains("telegram-web-app.js", appShell, StringComparison.Ordinal);
        Assert.Contains("window.authenticateTelegramMiniApp", appShell, StringComparison.Ordinal);
        Assert.Contains("Telegram.WebApp.initData", appShell, StringComparison.Ordinal);
        Assert.Contains("window.waitForTelegramMiniAppInitData", appShell, StringComparison.Ordinal);
        Assert.Contains("window.watchTelegramMiniAppLogin", appShell, StringComparison.Ordinal);
        Assert.Contains("window.handleTelegramLogin", appShell, StringComparison.Ordinal);
        Assert.Contains("/auth/telegram-login", appShell, StringComparison.Ordinal);
        Assert.Contains("data-onauth", appShell, StringComparison.Ordinal);
        Assert.DoesNotContain("data-auth-url", appShell, StringComparison.Ordinal);
        Assert.Contains("setTimeout(resolve, 100)", appShell, StringComparison.Ordinal);
        Assert.Contains("reloadOnReturn", appShell, StringComparison.Ordinal);
        Assert.Contains("gmRelayMiniAppLoginLeftPage", appShell, StringComparison.Ordinal);
        Assert.Contains("window.location.reload()", appShell, StringComparison.Ordinal);
        Assert.Contains("syncTelegramMiniAppViewport", appShell, StringComparison.Ordinal);
        Assert.Contains("safeAreaChanged", appShell, StringComparison.Ordinal);
        Assert.Contains("contentSafeAreaChanged", appShell, StringComparison.Ordinal);
        Assert.Contains("viewportChanged", appShell, StringComparison.Ordinal);
    }

    [Fact]
    public async Task Program_ShouldMapTelegramWebAppAuthEndpoint()
    {
        var program = await File.ReadAllTextAsync(FindRepositoryFile("src/GmRelay.Web/Program.cs"));

        Assert.Contains("MapPost(\"/auth/telegram-webapp\"", program, StringComparison.Ordinal);
        Assert.Contains("MapPost(\"/auth/telegram-login\"", program, StringComparison.Ordinal);
        Assert.Contains("VerifyWebAppInitData", program, StringComparison.Ordinal);
        Assert.Contains("VerifyLoginPayload", program, StringComparison.Ordinal);
        Assert.Contains("MapGet(\"/auth/status\"", program, StringComparison.Ordinal);
        Assert.Contains("authenticated", program, StringComparison.Ordinal);
    }

    [Fact]
    public async Task Styles_ShouldIncludeMiniAppMobileDashboardRules()
    {
        var css = await File.ReadAllTextAsync(FindRepositoryFile("src/GmRelay.Web/wwwroot/app.css"));

        Assert.Contains("mini-app-page", css, StringComparison.Ordinal);
        Assert.Contains("mini-app-auth-card", css, StringComparison.Ordinal);
        Assert.Contains("@media (max-width: 768px)", css, StringComparison.Ordinal);
        Assert.Contains("--tg-safe-area-inset-top", css, StringComparison.Ordinal);
        Assert.Contains("--tg-content-safe-area-inset-top", css, StringComparison.Ordinal);
        Assert.Contains(".telegram-mini-app .nav-header", css, StringComparison.Ordinal);
        Assert.Contains(".telegram-mini-app .nav-toggle", css, StringComparison.Ordinal);
        Assert.Contains(".telegram-mini-app .mini-app-page", css, StringComparison.Ordinal);
    }

    [Fact]
    public async Task LoginPage_ShouldAuthenticateMiniAppFallbackInsideCurrentWebView()
    {
        var loginPage = await File.ReadAllTextAsync(FindRepositoryFile("src/GmRelay.Web/Components/Pages/Login.razor"));

        Assert.Contains(
            "JS.InvokeVoidAsync(\"loadTelegramWidget\", BotUsername, \"/auth/telegram-login\")",
            loginPage,
            StringComparison.Ordinal);
        Assert.Contains(
            "JS.InvokeVoidAsync(\"watchTelegramMiniAppLogin\", \"/auth/status\", \"/\", false)",
            loginPage,
            StringComparison.Ordinal);
    }

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

            directory = directory.Parent;
        }

        throw new FileNotFoundException($"Could not locate repository file '{relativePath}'.");
    }
}
