using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Admin.Security;

namespace ZB.MOM.WW.OtOpcUa.Admin.Tests;

[Trait("Category", "Unit")]
public sealed class RoleMapperTests
{
    [Fact]
    public void Maps_single_group_to_single_role()
    {
        var mapping = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
        {
            ["ReadOnly"] = "ConfigViewer",
        };
        RoleMapper.Map(["ReadOnly"], mapping).ShouldBe(["ConfigViewer"]);
    }

    [Fact]
    public void Group_match_is_case_insensitive()
    {
        var mapping = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
        {
            ["ReadOnly"] = "ConfigViewer",
        };
        RoleMapper.Map(["readonly"], mapping).ShouldContain("ConfigViewer");
    }

    [Fact]
    public void User_with_multiple_matching_groups_gets_all_distinct_roles()
    {
        var mapping = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
        {
            ["ReadOnly"] = "ConfigViewer",
            ["ReadWrite"] = "ConfigEditor",
            ["AlarmAck"] = "FleetAdmin",
        };
        var roles = RoleMapper.Map(["ReadOnly", "ReadWrite", "AlarmAck"], mapping);
        roles.ShouldContain("ConfigViewer");
        roles.ShouldContain("ConfigEditor");
        roles.ShouldContain("FleetAdmin");
        roles.Count.ShouldBe(3);
    }

    [Fact]
    public void Unknown_group_is_ignored()
    {
        var mapping = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
        {
            ["ReadOnly"] = "ConfigViewer",
        };
        RoleMapper.Map(["UnrelatedGroup"], mapping).ShouldBeEmpty();
    }

    [Fact]
    public void Empty_mapping_returns_empty_roles()
    {
        RoleMapper.Map(["ReadOnly"], new Dictionary<string, string>()).ShouldBeEmpty();
    }
}
