namespace ZB.MOM.WW.OtOpcUa.Configuration.Entities;

/// <summary>
///     A scripted OPC UA Part 9 alarm whose
///     condition is the predicate <see cref="Script"/> referenced by
///     <see cref="PredicateScriptId"/>. Materialized by <c>Core.ScriptedAlarms</c> as a
///     concrete <c>AlarmConditionType</c> subtype per <see cref="AlarmType"/>.
/// </summary>
/// <remarks>
///     <para>
///         Message tokens (<c>{TagPath}</c>) resolved at emission time.
///         <see cref="HistorizeToAveva"/> gates whether transitions
///         route through the Core.AlarmHistorian SQLite queue + Galaxy.Host to the Aveva
///         Historian alarm schema.
///     </para>
/// </remarks>
public sealed class ScriptedAlarm
{
    /// <summary>Gets or sets the database row identifier for this scripted alarm.</summary>
    public Guid ScriptedAlarmRowId { get; set; }

    /// <summary>Stable logical id — drives <c>AlarmConditionType.ConditionName</c>. Globally unique in v2.</summary>
    public required string ScriptedAlarmId { get; set; }

    /// <summary>Logical FK to <see cref="Equipment.EquipmentId"/> — owner of this alarm.</summary>
    public required string EquipmentId { get; set; }

    /// <summary>Operator-facing alarm name.</summary>
    public required string Name { get; set; }

    /// <summary>Concrete Part 9 type — "AlarmCondition" / "LimitAlarm" / "OffNormalAlarm" / "DiscreteAlarm".</summary>
    public required string AlarmType { get; set; }

    /// <summary>Numeric severity 1..1000 per OPC UA Part 9 (usual bands: 1-250 Low, 251-500 Medium, 501-750 High, 751-1000 Critical).</summary>
    public int Severity { get; set; } = 500;

    /// <summary>Template with <c>{TagPath}</c> tokens resolved at emission time.</summary>
    public required string MessageTemplate { get; set; }

    /// <summary>Logical FK to <see cref="Script.ScriptId"/> — predicate script returning <c>bool</c>.</summary>
    public required string PredicateScriptId { get; set; }

    /// <summary>
    ///     When true, transitions route through the SQLite store-and-forward
    ///     queue to the Aveva Historian. Defaults on for scripted alarms because they are the
    ///     primary motivation for the historian sink; operator can disable per alarm.
    /// </summary>
    public bool HistorizeToAveva { get; set; } = true;

    /// <summary>
    ///     OPC UA Part 9 <c>Retain</c> flag — whether the alarm keeps active-state between
    ///     sessions. Most plant alarms are retained; one-shot event-style alarms are not.
    /// </summary>
    public bool Retain { get; set; } = true;

    /// <summary>Gets or sets a value indicating whether this alarm is enabled.</summary>
    public bool Enabled { get; set; } = true;

    /// <summary>Optimistic concurrency token for last-write-wins detection in the v2 live-edit model.</summary>
    public byte[] RowVersion { get; set; } = Array.Empty<byte>();
}
