using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;

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

/// <summary>Task-11: pure OPC UA A&C field → AlarmConditionState/transition mapping.</summary>
public class OpcUaAlarmMapperTests
{
    [Fact]
    public void NormalizeSeverity_ClampsTo0_1000()
    {
        Assert.Equal(1000, OpcUaAlarmMapper.NormalizeSeverity(5000));
        Assert.Equal(0, OpcUaAlarmMapper.NormalizeSeverity(-1));
        Assert.Equal(500, OpcUaAlarmMapper.NormalizeSeverity(500));
    }

    [Fact]
    public void BuildCondition_ActiveUnacked()
    {
        var c = OpcUaAlarmMapper.BuildCondition(active: true, acked: false, confirmed: null,
            shelve: AlarmShelveState.Unshelved, suppressed: false, severity: 700);
        Assert.True(c.Active);
        Assert.False(c.Acknowledged);
        Assert.Equal(700, c.Severity);
        Assert.Equal(AlarmShelveState.Unshelved, c.Shelve);
    }

    [Fact]
    public void DeriveKind_AckEdge_YieldsAcknowledge()
    {
        Assert.Equal(AlarmTransitionKind.Acknowledge,
            OpcUaAlarmMapper.DeriveKind(prevAcked: false, nowAcked: true, prevActive: true, nowActive: true));
    }

    [Fact]
    public void DeriveKind_ActiveEdge_YieldsRaise()
    {
        Assert.Equal(AlarmTransitionKind.Raise,
            OpcUaAlarmMapper.DeriveKind(prevAcked: true, nowAcked: true, prevActive: false, nowActive: true));
    }

    [Fact]
    public void DeriveKind_InactiveEdge_YieldsClear()
    {
        Assert.Equal(AlarmTransitionKind.Clear,
            OpcUaAlarmMapper.DeriveKind(prevAcked: true, nowAcked: true, prevActive: true, nowActive: false));
    }

    [Theory]
    [InlineData("OneShotShelved", AlarmShelveState.OneShotShelved)]
    [InlineData("TimedShelved", AlarmShelveState.TimedShelved)]
    [InlineData("Unshelved", AlarmShelveState.Unshelved)]
    [InlineData(null, AlarmShelveState.Unshelved)]
    public void MapShelve_MapsCurrentStateName(string? name, AlarmShelveState expected)
    {
        Assert.Equal(expected, OpcUaAlarmMapper.MapShelve(name));
    }
}
