using System.Collections.Generic;
using ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Historian;
///
/// Binds the AlarmHistorian configuration section that gates the durable
/// store-and-forward alarm sink. When is true,
/// AddAlarmHistorian registers a LocalDbStoreAndForwardSink (draining to the
/// gateway alarm writer supplied by the Host) in place of the
/// NullAlarmHistorianSink default; otherwise the Null default survives. This section
/// supplies only the gate and the store-and-forward knobs — the
/// downstream connection (endpoint/key/TLS) is sourced from the ServerHistorian section,
/// and the queue's storage location is the node's consolidated LocalDb:Path database.
///
public sealed class AlarmHistorianOptions
{
/// The configuration section name this options class binds.
public const string SectionName = "AlarmHistorian";
///
/// When true, the durable SQLite store-and-forward sink is registered; when
/// false (the default) the no-op NullAlarmHistorianSink stays in place.
///
public bool Enabled { get; init; }
/// Maximum number of queued rows the drain worker forwards in a single batch.
public int BatchSize { get; init; } = 100;
/// Seconds between drain-worker ticks. Defaults to 5.
public int DrainIntervalSeconds { get; init; } = 5;
/// Maximum queued rows before the sink evicts the oldest. Defaults to 1,000,000
/// (matches LocalDbStoreAndForwardSink's DefaultCapacity).
public long Capacity { get; init; } = LocalDbStoreAndForwardSink.DefaultCapacity;
/// Days to retain dead-lettered rows before purge. Defaults to 30.
public int DeadLetterRetentionDays { get; init; } = 30;
/// Maximum delivery attempts before a perpetually-retrying (poison) row is dead-lettered.
/// Defaults to 10 (matches LocalDbStoreAndForwardSink's DefaultMaxAttempts).
public int MaxAttempts { get; init; } = LocalDbStoreAndForwardSink.DefaultMaxAttempts;
/// Returns operator-facing misconfiguration warnings for an Enabled historian
/// (empty when disabled or correctly configured). Pure — the registration logs each entry.
/// Zero or more human-readable warning messages.
public IReadOnlyList Validate()
{
var warnings = new List();
if (!Enabled) return warnings;
if (DrainIntervalSeconds <= 0)
warnings.Add($"AlarmHistorian:DrainIntervalSeconds is {DrainIntervalSeconds} — must be > 0; the drain timer will throw or spin at startup.");
if (Capacity <= 0)
warnings.Add($"AlarmHistorian:Capacity is {Capacity} — must be > 0; the sink constructor will throw at startup.");
if (DeadLetterRetentionDays <= 0)
warnings.Add($"AlarmHistorian:DeadLetterRetentionDays is {DeadLetterRetentionDays} — must be > 0; dead-lettered rows would be purged on every drain tick.");
if (MaxAttempts <= 0)
warnings.Add($"AlarmHistorian:MaxAttempts is {MaxAttempts} — must be > 0; the sink constructor will throw at startup.");
return warnings;
}
}