namespace ZB.MOM.WW.Theme.Tests;

public class LoginCardTests : TestContext
{
    [Fact]
    public void Posts_to_action_with_username_password_fields()
    {
        var cut = RenderComponent<LoginCard>(p => p
            .Add(x => x.Product, "OtOpcUa")
            .Add(x => x.Action, "/auth/login"));
        var form = cut.Find("form");
        Assert.Equal("post", form.GetAttribute("method"));
        Assert.Equal("/auth/login", form.GetAttribute("action"));
        Assert.NotNull(cut.Find("input#username"));
        Assert.NotNull(cut.Find("input#password"));
        Assert.Contains("OtOpcUa", cut.Find(".login-title").TextContent);
    }

    [Fact]
    public void ReturnUrl_renders_hidden_input()
    {
        var cut = RenderComponent<LoginCard>(p => p
            .Add(x => x.Product, "OtOpcUa")
            .Add(x => x.ReturnUrl, "/clusters"));
        var hidden = cut.Find("input[name=returnUrl]");
        Assert.Equal("/clusters", hidden.GetAttribute("value"));
    }

    [Fact]
    public void Error_renders_notice()
    {
        var cut = RenderComponent<LoginCard>(p => p
            .Add(x => x.Product, "OtOpcUa")
            .Add(x => x.Error, "Bad credentials"));
        Assert.Contains("Bad credentials", cut.Find(".notice").TextContent);
    }

    [Fact]
    public void No_returnUrl_no_hidden_input()
    {
        var cut = RenderComponent<LoginCard>(p => p.Add(x => x.Product, "OtOpcUa"));
        Assert.Empty(cut.FindAll("input[name=returnUrl]"));
    }

    // Theme issue #5: the product token is isolated in a .login-product span so it
    // can be styled/asserted apart from the "— sign in" suffix.
    [Fact]
    public void Product_is_wrapped_in_login_product_span()
    {
        var cut = RenderComponent<LoginCard>(p => p.Add(x => x.Product, "OtOpcUa"));
        var product = cut.Find(".login-title .login-product");
        Assert.Equal("OtOpcUa", product.TextContent);
        Assert.Contains("sign in", cut.Find(".login-title").TextContent);
    }

    // Theme issue #5: Heading replaces the whole heading copy when set.
    [Fact]
    public void Heading_overrides_default_heading_when_set()
    {
        var cut = RenderComponent<LoginCard>(p => p
            .Add(x => x.Product, "OtOpcUa")
            .Add(x => x.Heading, "Welcome back"));
        var title = cut.Find(".login-title");
        Assert.Equal("Welcome back", title.TextContent);
        Assert.Empty(cut.FindAll(".login-title .login-product"));
        Assert.DoesNotContain("sign in", title.TextContent);
    }
}
