using System.Xml.Linq;

namespace GmRelay.Bot.Tests.Web;

public sealed class CampaignTemplatesNavigationTests
{
    [Fact]
    public async Task NavMenu_ShouldExposeTemplatesTab()
    {
        var navMenu = await File.ReadAllTextAsync(FindRepositoryFile("src/GmRelay.Web/Components/Layout/NavMenu.razor"));

        Assert.Contains("href=\"templates\"", navMenu, StringComparison.Ordinal);
        Assert.Contains("Шаблоны", navMenu, StringComparison.Ordinal);
    }

    [Fact]
    public async Task NavMenu_ShouldExposeCurrentProjectVersion()
    {
        var navMenu = await File.ReadAllTextAsync(FindRepositoryFile("src/GmRelay.Web/Components/Layout/NavMenu.razor"));
        var props = XDocument.Load(FindRepositoryFile("Directory.Build.props"));
        var version = props.Root?
            .Element("PropertyGroup")?
            .Element("Version")?
            .Value;

        Assert.False(string.IsNullOrWhiteSpace(version));
        Assert.Contains($"v{version}", navMenu, StringComparison.Ordinal);
    }

    [Fact]
    public async Task NavMenuStyles_ShouldStyleNavLinkAnchorsAsStackedRows()
    {
        var navCss = await File.ReadAllTextAsync(FindRepositoryFile("src/GmRelay.Web/Components/Layout/NavMenu.razor.css"));

        Assert.Contains("::deep .nav-item", navCss, StringComparison.Ordinal);
        Assert.Matches(
            @"\.nav-section\s*\{[^}]*display:\s*flex;[^}]*flex-direction:\s*column;[^}]*gap:\s*0\.25rem;",
            navCss);
        Assert.Matches(
            @"::deep\s+\.nav-item\s*\{[^}]*display:\s*flex;[^}]*width:\s*100%;",
            navCss);
    }

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

        Assert.Contains("CreateBatchFromTemplate", groupDetails, StringComparison.Ordinal);
        Assert.DoesNotContain("OnValidSubmit=\"CreateCampaignTemplate\"", groupDetails, StringComparison.Ordinal);
        Assert.DoesNotContain("DeleteCampaignTemplate", groupDetails, StringComparison.Ordinal);
    }

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

        Assert.Contains("@page \"/templates\"", templatesPage, StringComparison.Ordinal);
        Assert.Contains("OnValidSubmit=\"CreateCampaignTemplate\"", templatesPage, StringComparison.Ordinal);
        Assert.Contains("DeleteCampaignTemplate", templatesPage, 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}'.");
    }
}
