namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Host.Backend.Historian;

/// <summary>
///     Maps a raw OPC DA quality byte (as returned by Wonderware Historian's <c>OpcQuality</c>)
///     to an OPC UA <c>StatusCode</c> uint. Preserves specific codes (BadNotConnected,
///     UncertainSubNormal, etc.) instead of collapsing to Good/Uncertain/Bad categories.
///     Mirrors v1 <c>QualityMapper.MapToOpcUaStatusCode</c> without pulling in OPC UA types —
///     the returned value is the 32-bit OPC UA <c>StatusCode</c> wire encoding that the Proxy
///     surfaces directly as <c>DataValueSnapshot.StatusCode</c>.
/// </summary>
public static class HistorianQualityMapper
{
    /// <summary>
    ///     Map an 8-bit OPC DA quality byte to the corresponding OPC UA StatusCode. The byte
    ///     family bits decide the category (Good &gt;= 192, Uncertain 64-191, Bad 0-63); the
    ///     low-nibble subcode selects the specific code.
    /// </summary>
    public static uint Map(byte q) => q switch
    {
        // Good family (192+)
        192 => 0x00000000u,             // Good
        216 => 0x00D80000u,             // Good_LocalOverride

        // Uncertain family (64-191)
        64 => 0x40000000u,              // Uncertain
        68 => 0x40900000u,              // Uncertain_LastUsableValue
        80 => 0x40930000u,              // Uncertain_SensorNotAccurate
        84 => 0x40940000u,              // Uncertain_EngineeringUnitsExceeded
        88 => 0x40950000u,              // Uncertain_SubNormal

        // Bad family (0-63)
        0  => 0x80000000u,              // Bad
        4  => 0x80890000u,              // Bad_ConfigurationError
        8  => 0x808A0000u,              // Bad_NotConnected
        12 => 0x808B0000u,              // Bad_DeviceFailure
        16 => 0x808C0000u,              // Bad_SensorFailure
        20 => 0x80050000u,              // Bad_CommunicationError
        24 => 0x808D0000u,              // Bad_OutOfService
        32 => 0x80320000u,              // Bad_WaitingForInitialData

        // Unknown code — fall back to the category so callers still get a sensible bucket.
        _ when q >= 192 => 0x00000000u,
        _ when q >= 64 => 0x40000000u,
        _ => 0x80000000u,
    };
}
