using Opc.Ua;
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;

namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests;

/// <summary>
/// M7-B1 (T16 type-info): pure NodeId → friendly built-in DataType name mapping.
/// Known OPC UA built-in DataType NodeIds (ns=0;i=…) resolve to their CLR-ish
/// friendly names; unknown/custom DataType NodeIds fall back to the NodeId string.
/// No live server is required — this is a pure lookup helper.
/// </summary>
public class OpcUaDataTypeNameMapTests
{
    [Theory]
    [InlineData("Boolean")]
    [InlineData("SByte")]
    [InlineData("Byte")]
    [InlineData("Int16")]
    [InlineData("UInt16")]
    [InlineData("Int32")]
    [InlineData("UInt32")]
    [InlineData("Int64")]
    [InlineData("UInt64")]
    [InlineData("Float")]
    [InlineData("Double")]
    [InlineData("String")]
    [InlineData("DateTime")]
    [InlineData("Guid")]
    [InlineData("ByteString")]
    public void Resolve_KnownBuiltInTypes_ReturnFriendlyNames(string typeName)
    {
        // Look up the well-known DataType NodeId by its standard name on
        // DataTypeIds (e.g. DataTypeIds.Double) so the test mirrors exactly
        // the constants the helper maps against.
        var field = typeof(DataTypeIds).GetField(typeName)!;
        var nodeId = (NodeId)field.GetValue(null)!;

        Assert.Equal(typeName, OpcUaBuiltInTypeNames.Resolve(nodeId));
    }

    [Fact]
    public void Resolve_Double_ReturnsDouble()
    {
        Assert.Equal("Double", OpcUaBuiltInTypeNames.Resolve(DataTypeIds.Double));
    }

    [Fact]
    public void Resolve_Int32_ReturnsInt32()
    {
        Assert.Equal("Int32", OpcUaBuiltInTypeNames.Resolve(DataTypeIds.Int32));
    }

    [Fact]
    public void Resolve_Boolean_ReturnsBoolean()
    {
        Assert.Equal("Boolean", OpcUaBuiltInTypeNames.Resolve(DataTypeIds.Boolean));
    }

    [Fact]
    public void Resolve_String_ReturnsString()
    {
        Assert.Equal("String", OpcUaBuiltInTypeNames.Resolve(DataTypeIds.String));
    }

    [Fact]
    public void Resolve_UnknownCustomNodeId_ReturnsNodeIdString()
    {
        // A vendor-defined DataType (string identifier, non-zero namespace) is
        // not a built-in, so the helper falls back to the NodeId string — the
        // only thing the UI can meaningfully display for an opaque type.
        var custom = new NodeId("CustomType", 5);

        Assert.Equal(custom.ToString(), OpcUaBuiltInTypeNames.Resolve(custom));
    }

    [Fact]
    public void Resolve_Null_ReturnsEmptyString()
    {
        // Defensive: a null DataType attribute value must not throw during a
        // best-effort browse type-read.
        Assert.Equal(string.Empty, OpcUaBuiltInTypeNames.Resolve(null));
    }
}
