using ZB.MOM.WW.ScadaBridge.Commons.Messages.DataConnection;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Alarms;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;

namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Messages;

public class NativeAlarmMessagesTests
{
    [Fact]
    public void SubscribeAlarmsRequest_CarriesSourceAndFilter()
    {
        var r = new SubscribeAlarmsRequest("c1", "inst", "PlantOpcUa", "ns=2;s=Tank01", null, DateTimeOffset.UnixEpoch);
        Assert.Equal("ns=2;s=Tank01", r.SourceReference);
        Assert.Null(r.ConditionFilter);
    }

    [Fact]
    public void NativeAlarmTransitionUpdate_WrapsTransition()
    {
        var t = new NativeAlarmTransition("Tank01.Hi", "Tank01", "x", AlarmTransitionKind.Raise,
            new AlarmConditionState(true, false, null, AlarmShelveState.Unshelved, false, 500),
            "", "", "", "", "", null, DateTimeOffset.UnixEpoch, "", "");
        var u = new NativeAlarmTransitionUpdate("PlantOpcUa", t);
        Assert.Equal("PlantOpcUa", u.ConnectionName);
        Assert.Equal("Tank01", u.Transition.SourceObjectReference);
    }

    [Fact]
    public void NativeAlarmTransition_AckTime_IsAnAdditiveTrailingParameter()
    {
        // MES alarm-status API §6.4. The 14-argument positional form (every pre-existing
        // call site) must still compile and report no ack time; the 15th argument is the
        // only way to set one.
        var withoutAck = new NativeAlarmTransition("Tank01.Hi", "Tank01", "x", AlarmTransitionKind.Raise,
            new AlarmConditionState(true, false, null, AlarmShelveState.Unshelved, false, 500),
            "", "", "", "", "", null, DateTimeOffset.UnixEpoch, "", "");
        Assert.Null(withoutAck.AckTime);

        var ackedAt = DateTimeOffset.UnixEpoch.AddMinutes(5);
        var withAck = new NativeAlarmTransition("Tank01.Hi", "Tank01", "x", AlarmTransitionKind.Acknowledge,
            new AlarmConditionState(true, true, null, AlarmShelveState.Unshelved, false, 500),
            "", "", "", "", "", null, DateTimeOffset.UnixEpoch, "", "", ackedAt);
        Assert.Equal(ackedAt, withAck.AckTime);
    }
}
