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

// Skip/Take are additive offset paging (arch-review P2), applied AFTER in-memory
// site-scope filtering. Take = null preserves the historical unpaged behaviour.
public record ListInstancesCommand(int? SiteId = null, int? TemplateId = null, string? SearchTerm = null, int Skip = 0, int? Take = null);
public record GetInstanceCommand(int InstanceId);
public record CreateInstanceCommand(string UniqueName, int TemplateId, int SiteId, int? AreaId = null);
public record MgmtDeployInstanceCommand(int InstanceId);
public record MgmtEnableInstanceCommand(int InstanceId);
public record MgmtDisableInstanceCommand(int InstanceId);
public record MgmtDeleteInstanceCommand(int InstanceId);
/// <summary>
/// A single attribute-to-data-connection binding carried by
/// <see cref="SetConnectionBindingsCommand"/>. This is a named record (not a
/// <c>ValueTuple</c>) so it serializes with stable, named JSON properties and can
/// evolve additively per REQ-COM-5a.
/// <para>
/// <c>DataSourceReferenceOverride</c> is an optional per-instance override of
/// the OPC UA node id (or other protocol address) for the bound attribute.
/// When non-null it replaces the template's <c>DataSourceReference</c> at
/// flattening time; when null the template default is used.
/// </para>
/// </summary>
public record ConnectionBinding(
    string AttributeName,
    int DataConnectionId,
    string? DataSourceReferenceOverride = null);

public record SetConnectionBindingsCommand(int InstanceId, IReadOnlyList<ConnectionBinding> Bindings);
public record SetInstanceOverridesCommand(int InstanceId, IReadOnlyDictionary<string, string?> Overrides);
public record SetInstanceAreaCommand(int InstanceId, int? AreaId);

/// <summary>
/// Sets (or upserts) a per-instance alarm override. For HiLo trigger types the
/// <c>TriggerConfigurationOverride</c> JSON is merged setpoint-by-setpoint with
/// the inherited config; for binary trigger types it replaces the whole config.
/// Either field is optional — pass null to leave it unchanged.
/// </summary>
public record SetInstanceAlarmOverrideCommand(
    int InstanceId,
    string AlarmCanonicalName,
    string? TriggerConfigurationOverride,
    int? PriorityLevelOverride);

public record DeleteInstanceAlarmOverrideCommand(
    int InstanceId,
    string AlarmCanonicalName);

public record ListInstanceAlarmOverridesCommand(int InstanceId);

// Native alarm source overrides (per-instance retarget of an inherited source binding;
// null fields keep the inherited value).
public record SetInstanceNativeAlarmSourceOverrideCommand(
    int InstanceId,
    string SourceCanonicalName,
    string? ConnectionNameOverride,
    string? SourceReferenceOverride,
    string? ConditionFilterOverride);

public record DeleteInstanceNativeAlarmSourceOverrideCommand(
    int InstanceId,
    string SourceCanonicalName);

public record ListInstanceNativeAlarmSourceOverridesCommand(int InstanceId);

/// <summary>
/// One entry in a bulk native-alarm-source-override apply: the source binding's
/// canonical name plus the three optional retarget fields (null keeps the inherited
/// value). Mirrors <see cref="SetInstanceNativeAlarmSourceOverrideCommand"/>'s fields
/// for a single source.
/// </summary>
public record NativeAlarmSourceOverrideEntry(
    string SourceCanonicalName,
    string? ConnectionNameOverride,
    string? SourceReferenceOverride,
    string? ConditionFilterOverride);

/// <summary>
/// Bulk, all-or-nothing apply of per-instance native-alarm-source overrides — the
/// native-source parity analogue of <see cref="SetInstanceOverridesCommand"/>. Backs
/// the CLI <c>instance native-alarm-source import --file</c> CSV path. Every entry's
/// source must resolve for the instance and be unlocked; if any entry is invalid the
/// whole batch is rejected before any write.
/// </summary>
public record SetInstanceNativeAlarmSourceOverridesCommand(
    int InstanceId,
    IReadOnlyList<NativeAlarmSourceOverrideEntry> Overrides);
