using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns.TagEditors;

namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;

/// <summary>
/// Tests for <see cref="NativeAlarmModel.SeedDefaultAlarm"/> — the Galaxy-picker convenience that
/// pre-fills a default native-alarm <c>alarm</c> object when an <c>IsAlarm</c> attribute is selected.
/// Seeds only when absent (never overwrites an authored alarm) and preserves every other key.
/// </summary>
public sealed class NativeAlarmSeedTests
{
    [Fact]
    public void Seeds_default_alarm_when_absent()
    {
        var seeded = NativeAlarmModel.SeedDefaultAlarm("""{"FullName":"Pump_001.HiHi"}""");

        var m = NativeAlarmModel.FromJson(seeded);
        m.IsAlarm.ShouldBeTrue();
        m.AlarmType.ShouldBe(NativeAlarmModel.DefaultSeedAlarmType);   // OffNormalAlarm
        m.Severity.ShouldBe(NativeAlarmModel.DefaultSeedSeverity);     // 700
        // FullName is preserved alongside the freshly-seeded alarm.
        seeded.ShouldContain("FullName");
        seeded.ShouldContain("Pump_001.HiHi");
    }

    [Fact]
    public void Does_not_overwrite_an_existing_alarm_object()
    {
        const string authored =
            """{"FullName":"Pump_001.HiHi","alarm":{"alarmType":"LimitAlarm","severity":250,"historizeToAveva":false}}""";

        var result = NativeAlarmModel.SeedDefaultAlarm(authored);

        var m = NativeAlarmModel.FromJson(result);
        // The authored alarm is left untouched — not replaced with the default seed.
        m.AlarmType.ShouldBe("LimitAlarm");
        m.Severity.ShouldBe(250);
        m.HistorizeToAveva.ShouldBe(false);
    }
}
