using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.Historian.Wonderware.Backend;

namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Host.Tests;

[Trait("Category", "Unit")]
public sealed class HistorianQualityMapperTests
{
    /// <summary>
    ///     Rich mapping preserves specific OPC DA subcodes through the historian ToWire path.
    ///     Before PR 12 the category-only fallback collapsed e.g. BadNotConnected(8) to
    ///     Bad(0x80000000) so downstream OPC UA clients could not distinguish transport issues
    ///     from sensor issues. After PR 12 every known subcode round-trips to its canonical
    ///     uint32 StatusCode and Proxy translation stays byte-for-byte with v1 QualityMapper.
    /// </summary>
    /// <param name="quality">The OPC DA quality code to map.</param>
    /// <param name="expected">The expected canonical OPC UA StatusCode.</param>
    [Theory]
    [InlineData((byte)192, 0x00000000u)] // Good
    [InlineData((byte)216, 0x00D80000u)] // Good_LocalOverride
    [InlineData((byte)64, 0x40000000u)] // Uncertain
    [InlineData((byte)68, 0x40900000u)] // Uncertain_LastUsableValue
    [InlineData((byte)80, 0x40930000u)] // Uncertain_SensorNotAccurate
    [InlineData((byte)84, 0x40940000u)] // Uncertain_EngineeringUnitsExceeded
    [InlineData((byte)88, 0x40950000u)] // Uncertain_SubNormal
    [InlineData((byte)0, 0x80000000u)] // Bad
    [InlineData((byte)4, 0x80890000u)] // Bad_ConfigurationError
    [InlineData((byte)8, 0x808A0000u)] // Bad_NotConnected
    [InlineData((byte)12, 0x808B0000u)] // Bad_DeviceFailure
    [InlineData((byte)16, 0x808C0000u)] // Bad_SensorFailure
    [InlineData((byte)20, 0x80050000u)] // Bad_CommunicationError
    [InlineData((byte)24, 0x808D0000u)] // Bad_OutOfService
    [InlineData((byte)32, 0x80320000u)] // Bad_WaitingForInitialData
    public void Maps_specific_OPC_DA_codes_to_canonical_StatusCode(byte quality, uint expected)
    {
        HistorianQualityMapper.Map(quality).ShouldBe(expected);
    }

    /// <summary>Verifies that unknown good-family quality codes fall back to plain Good.</summary>
    /// <param name="q">The OPC DA quality byte to test.</param>
    [Theory]
    [InlineData((byte)200)] // Good — unknown subcode in Good family
    [InlineData((byte)255)] // Good — unknown
    public void Unknown_good_family_codes_fall_back_to_plain_Good(byte q)
    {
        HistorianQualityMapper.Map(q).ShouldBe(0x00000000u);
    }

    /// <summary>Verifies that unknown uncertain-family quality codes fall back to plain Uncertain.</summary>
    /// <param name="q">The OPC DA quality byte to test.</param>
    [Theory]
    [InlineData((byte)100)] // Uncertain — unknown subcode
    [InlineData((byte)150)] // Uncertain — unknown
    public void Unknown_uncertain_family_codes_fall_back_to_plain_Uncertain(byte q)
    {
        HistorianQualityMapper.Map(q).ShouldBe(0x40000000u);
    }

    /// <summary>Verifies that unknown bad-family quality codes fall back to plain Bad.</summary>
    /// <param name="q">The OPC DA quality byte to test.</param>
    [Theory]
    [InlineData((byte)1)] // Bad — unknown subcode
    [InlineData((byte)50)] // Bad — unknown
    public void Unknown_bad_family_codes_fall_back_to_plain_Bad(byte q)
    {
        HistorianQualityMapper.Map(q).ShouldBe(0x80000000u);
    }
}
