using FluentAssertions;
using Xunit;
using ZB.MOM.WW.LmxProxy.Host.Domain;

namespace ZB.MOM.WW.LmxProxy.Host.Tests.Domain
{
    public class QualityCodeMapperTests
    {
        [Theory]
        [InlineData(Quality.Good, 0x00000000u, "Good")]
        [InlineData(Quality.Good_LocalOverride, 0x00D80000u, "GoodLocalOverride")]
        [InlineData(Quality.Bad, 0x80000000u, "Bad")]
        [InlineData(Quality.Bad_ConfigError, 0x80040000u, "BadConfigurationError")]
        [InlineData(Quality.Bad_NotConnected, 0x808A0000u, "BadNotConnected")]
        [InlineData(Quality.Bad_DeviceFailure, 0x806B0000u, "BadDeviceFailure")]
        [InlineData(Quality.Bad_SensorFailure, 0x806D0000u, "BadSensorFailure")]
        [InlineData(Quality.Bad_CommFailure, 0x80050000u, "BadCommunicationFailure")]
        [InlineData(Quality.Bad_OutOfService, 0x808F0000u, "BadOutOfService")]
        [InlineData(Quality.Bad_WaitingForInitialData, 0x80320000u, "BadWaitingForInitialData")]
        [InlineData(Quality.Uncertain_LastUsable, 0x40900000u, "UncertainLastUsableValue")]
        [InlineData(Quality.Uncertain_SensorNotAcc, 0x42390000u, "UncertainSensorNotAccurate")]
        [InlineData(Quality.Uncertain_EuExceeded, 0x40540000u, "UncertainEngineeringUnitsExceeded")]
        [InlineData(Quality.Uncertain_SubNormal, 0x40580000u, "UncertainSubNormal")]
        public void ToQualityCode_MapsCorrectly(Quality quality, uint expectedStatusCode, string expectedName)
        {
            var qc = QualityCodeMapper.ToQualityCode(quality);
            qc.StatusCode.Should().Be(expectedStatusCode);
            qc.SymbolicName.Should().Be(expectedName);
        }

        [Theory]
        [InlineData(0x00000000u, Quality.Good)]
        [InlineData(0x80000000u, Quality.Bad)]
        [InlineData(0x80040000u, Quality.Bad_ConfigError)]
        [InlineData(0x806D0000u, Quality.Bad_SensorFailure)]
        [InlineData(0x40900000u, Quality.Uncertain_LastUsable)]
        public void FromStatusCode_MapsCorrectly(uint statusCode, Quality expectedQuality)
        {
            QualityCodeMapper.FromStatusCode(statusCode).Should().Be(expectedQuality);
        }

        [Fact]
        public void FromStatusCode_UnknownGoodCode_FallsBackToGood()
        {
            QualityCodeMapper.FromStatusCode(0x00FF0000).Should().Be(Quality.Good);
        }

        [Fact]
        public void FromStatusCode_UnknownBadCode_FallsBackToBad()
        {
            QualityCodeMapper.FromStatusCode(0x80FF0000).Should().Be(Quality.Bad);
        }

        [Fact]
        public void FromStatusCode_UnknownUncertainCode_FallsBackToUncertain()
        {
            QualityCodeMapper.FromStatusCode(0x40FF0000).Should().Be(Quality.Uncertain);
        }

        [Theory]
        [InlineData(0x00000000u, "Good")]
        [InlineData(0x80000000u, "Bad")]
        [InlineData(0x806D0000u, "BadSensorFailure")]
        [InlineData(0x40900000u, "UncertainLastUsableValue")]
        [InlineData(0x80FF0000u, "Bad")]  // unknown bad code falls back
        public void GetSymbolicName_ReturnsCorrectName(uint statusCode, string expectedName)
        {
            QualityCodeMapper.GetSymbolicName(statusCode).Should().Be(expectedName);
        }

        [Fact]
        public void FactoryMethods_ReturnCorrectCodes()
        {
            QualityCodeMapper.Good().StatusCode.Should().Be(0x00000000u);
            QualityCodeMapper.Bad().StatusCode.Should().Be(0x80000000u);
            QualityCodeMapper.BadConfigurationError().StatusCode.Should().Be(0x80040000u);
            QualityCodeMapper.BadCommunicationFailure().StatusCode.Should().Be(0x80050000u);
            QualityCodeMapper.BadNotConnected().StatusCode.Should().Be(0x808A0000u);
            QualityCodeMapper.BadDeviceFailure().StatusCode.Should().Be(0x806B0000u);
            QualityCodeMapper.BadSensorFailure().StatusCode.Should().Be(0x806D0000u);
            QualityCodeMapper.BadOutOfService().StatusCode.Should().Be(0x808F0000u);
            QualityCodeMapper.BadWaitingForInitialData().StatusCode.Should().Be(0x80320000u);
            QualityCodeMapper.GoodLocalOverride().StatusCode.Should().Be(0x00D80000u);
            QualityCodeMapper.UncertainLastUsableValue().StatusCode.Should().Be(0x40900000u);
        }
    }
}
