namespace ZB.MOM.WW.Theme.Tests;

public class NavRailTests : TestContext
{
    [Fact]
    public void NavRailItem_renders_rail_link_with_href_and_text()
    {
        var cut = RenderComponent<NavRailItem>(p => p
            .Add(x => x.Href, "/clusters")
            .Add(x => x.Text, "Clusters"));
        var a = cut.Find("a.rail-link");
        Assert.Equal("/clusters", a.GetAttribute("href"));
        Assert.Contains("Clusters", a.TextContent);
    }

    // Theme-004: when Icon is supplied it is wrapped in a .rail-ico span (now styled).
    [Fact]
    public void NavRailItem_wraps_icon_in_rail_ico_span_when_supplied()
    {
        var cut = RenderComponent<NavRailItem>(p => p
            .Add(x => x.Href, "/clusters")
            .Add(x => x.Text, "Clusters")
            .Add(x => x.Icon, (RenderFragment)(b => b.AddMarkupContent(0, "<svg class='ico'/>"))));
        var ico = cut.Find("a.rail-link .rail-ico");
        Assert.NotNull(ico);
        Assert.NotNull(cut.Find("a.rail-link .rail-ico .ico"));
    }

    [Fact]
    public void NavRailItem_omits_rail_ico_span_when_no_icon()
    {
        var cut = RenderComponent<NavRailItem>(p => p
            .Add(x => x.Href, "/clusters")
            .Add(x => x.Text, "Clusters"));
        Assert.Empty(cut.FindAll(".rail-ico"));
    }

    [Fact]
    public void NavRailSection_renders_title_and_children_open_by_default()
    {
        var cut = RenderComponent<NavRailSection>(p => p
            .Add(x => x.Title, "Navigation")
            .AddChildContent("<a class='rail-link'>X</a>"));
        var details = cut.Find("details.rail-section");
        Assert.True(details.HasAttribute("open"));
        Assert.Contains("Navigation", cut.Find("summary").TextContent);
        Assert.NotNull(cut.Find(".rail-section-body .rail-link"));
    }

    // Theme issue #1: the <summary> mirrors the <details open> state via
    // aria-expanded so tests and assistive tech have a stable, queryable attribute.
    [Fact]
    public void NavRailSection_summary_aria_expanded_true_when_open()
    {
        var cut = RenderComponent<NavRailSection>(p => p
            .Add(x => x.Title, "Navigation")
            .AddChildContent("<a class='rail-link'>X</a>"));
        Assert.Equal("true", cut.Find("summary.rail-eyebrow-toggle").GetAttribute("aria-expanded"));
    }

    [Fact]
    public void NavRailSection_collapsed_when_not_expanded()
    {
        var cut = RenderComponent<NavRailSection>(p => p
            .Add(x => x.Title, "Nav").Add(x => x.Expanded, false)
            .AddChildContent("<a class='rail-link'>X</a>"));
        Assert.False(cut.Find("details.rail-section").HasAttribute("open"));
    }

    // Theme issue #1: aria-expanded reflects the collapsed SSR state too.
    [Fact]
    public void NavRailSection_summary_aria_expanded_false_when_collapsed()
    {
        var cut = RenderComponent<NavRailSection>(p => p
            .Add(x => x.Title, "Nav").Add(x => x.Expanded, false)
            .AddChildContent("<a class='rail-link'>X</a>"));
        Assert.Equal("false", cut.Find("summary.rail-eyebrow-toggle").GetAttribute("aria-expanded"));
    }

    [Fact]
    public void NavRailSection_emits_data_nav_key_slug_from_title_by_default()
    {
        var cut = RenderComponent<NavRailSection>(p => p
            .Add(x => x.Title, "Site Calls")
            .AddChildContent("<a class='rail-link'>X</a>"));
        Assert.Equal("site-calls", cut.Find("details.rail-section").GetAttribute("data-nav-key"));
    }

    [Fact]
    public void NavRailSection_emits_explicit_key_when_supplied()
    {
        var cut = RenderComponent<NavRailSection>(p => p
            .Add(x => x.Title, "Navigation").Add(x => x.Key, "nav")
            .AddChildContent("<a class='rail-link'>X</a>"));
        Assert.Equal("nav", cut.Find("details.rail-section").GetAttribute("data-nav-key"));
    }

    [Fact]
    public void NavRailSection_whitespace_only_title_yields_empty_data_nav_key()
    {
        var cut = RenderComponent<NavRailSection>(p => p
            .Add(x => x.Title, "   ")
            .AddChildContent("<a class='rail-link'>X</a>"));
        Assert.Equal("", cut.Find("details.rail-section").GetAttribute("data-nav-key"));
    }

    [Fact]
    public void NavRailSection_slug_preserves_unicode_letters()
    {
        var cut = RenderComponent<NavRailSection>(p => p
            .Add(x => x.Title, "Café")
            .AddChildContent("<a class='rail-link'>X</a>"));
        Assert.Equal("café", cut.Find("details.rail-section").GetAttribute("data-nav-key"));
    }
}
