namespace ScadaLink.Commons.Entities.Instances;

/// <summary>
/// Per-instance override for a template-defined alarm. Lets a deployed
/// instance tweak setpoints, priority, or per-band messages without forking
/// the template. Locked alarms (TemplateAlarm.IsLocked) cannot be overridden
/// — LockEnforcer rejects the change at write time.
///
/// Merge semantics (applied during flattening, after template inheritance and
/// composition):
///   • <see cref="TriggerConfigurationOverride"/> with a HiLo trigger merges
///     into the inherited JSON setpoint-by-setpoint (derived keys win,
///     inherited keys survive for unset derived keys). Same logic as
///     template-to-template HiLo override, just one layer deeper.
///   • For ValueMatch / RangeViolation / RateOfChange, the override replaces
///     the whole TriggerConfiguration JSON (existing whole-replace semantics).
///   • <see cref="PriorityLevelOverride"/> replaces the alarm's PriorityLevel
///     when set.
/// </summary>
public class InstanceAlarmOverride
{
    public int Id { get; set; }
    public int InstanceId { get; set; }

    /// <summary>
    /// Canonical name of the alarm being overridden — matches
    /// <c>ResolvedAlarm.CanonicalName</c> after flattening, so composed-member
    /// alarms are referenced as <c>[CompositionInstance].[AlarmName]</c>.
    /// </summary>
    public string AlarmCanonicalName { get; set; }

    /// <summary>
    /// Partial JSON (for HiLo) or full JSON (for binary trigger types) to
    /// override the inherited TriggerConfiguration. <c>null</c> means
    /// "leave inherited as-is".
    /// </summary>
    public string? TriggerConfigurationOverride { get; set; }

    /// <summary>
    /// Replaces the alarm's PriorityLevel when set. <c>null</c> = keep inherited.
    /// </summary>
    public int? PriorityLevelOverride { get; set; }

    public InstanceAlarmOverride(string alarmCanonicalName)
    {
        AlarmCanonicalName = alarmCanonicalName ?? throw new ArgumentNullException(nameof(alarmCanonicalName));
    }
}
