using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.OpcUa;
using ZB.MOM.WW.OtOpcUa.Server.OpcUa;

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

[Trait("Category", "Unit")]
public sealed class DriverEquipmentContentRegistryTests
{
    private static readonly EquipmentNamespaceContent EmptyContent =
        new(Areas: [], Lines: [], Equipment: [], Tags: []);

    [Fact]
    public void Get_Returns_Null_For_Unknown_Driver()
    {
        var registry = new DriverEquipmentContentRegistry();
        registry.Get("galaxy-prod").ShouldBeNull();
        registry.Count.ShouldBe(0);
    }

    [Fact]
    public void Set_Then_Get_Returns_Stored_Content()
    {
        var registry = new DriverEquipmentContentRegistry();
        registry.Set("galaxy-prod", EmptyContent);

        registry.Get("galaxy-prod").ShouldBeSameAs(EmptyContent);
        registry.Count.ShouldBe(1);
    }

    [Fact]
    public void Get_Is_Case_Insensitive_For_Driver_Id()
    {
        // DriverInstanceId keys are OrdinalIgnoreCase across the codebase (Equipment /
        // Tag rows, walker grouping). Registry matches that contract so callers don't have
        // to canonicalize driver ids before lookup.
        var registry = new DriverEquipmentContentRegistry();
        registry.Set("Galaxy-Prod", EmptyContent);
        registry.Get("galaxy-prod").ShouldBeSameAs(EmptyContent);
        registry.Get("GALAXY-PROD").ShouldBeSameAs(EmptyContent);
    }

    [Fact]
    public void Set_Overwrites_Existing_Content_For_Same_Driver()
    {
        var registry = new DriverEquipmentContentRegistry();
        var first = EmptyContent;
        var second = new EquipmentNamespaceContent([], [], [], []);

        registry.Set("galaxy-prod", first);
        registry.Set("galaxy-prod", second);

        registry.Get("galaxy-prod").ShouldBeSameAs(second);
        registry.Count.ShouldBe(1);
    }
}
