namespace ZB.MOM.WW.ScadaBridge.NotificationOutbox;

/// <summary>
/// Configuration options for the Notification Outbox component: dispatch cadence,
/// batch sizing, stuck-message detection, terminal retention, and KPI windowing.
/// </summary>
public class NotificationOutboxOptions
{
    /// <summary>
    /// Interval between dispatch sweeps that pick up pending notifications for delivery.
    /// Default: 10 seconds.
    /// </summary>
    public TimeSpan DispatchInterval { get; set; } = TimeSpan.FromSeconds(10);

    /// <summary>
    /// Maximum number of notifications claimed for delivery in a single dispatch sweep.
    /// Caps per-sweep batch size to bound memory and per-iteration latency.
    /// Default: 100.
    /// </summary>
    public int DispatchBatchSize { get; set; } = 100;

    /// <summary>
    /// Age past which a still-<c>Pending</c>/<c>Retrying</c> notification is counted as
    /// "stuck" on the KPI tile and the per-row badge in the Central UI.
    /// Display-only: rows older than this threshold are flagged in KPIs/UI; there is no
    /// automatic re-claim, requeue, or escalation — the dispatcher behaviour is unaffected.
    /// Default: 10 minutes.
    /// </summary>
    public TimeSpan StuckAgeThreshold { get; set; } = TimeSpan.FromMinutes(10);

    /// <summary>
    /// Retention period for notifications in a terminal state (<c>Delivered</c>,
    /// <c>Parked</c>, <c>Discarded</c>) before they are purged from the Notifications table.
    /// Default: 365 days.
    /// </summary>
    public TimeSpan TerminalRetention { get; set; } = TimeSpan.FromDays(365);

    /// <summary>
    /// Interval between background purge sweeps of terminal notifications older than
    /// <see cref="TerminalRetention"/>.
    /// Default: 1 day.
    /// </summary>
    public TimeSpan PurgeInterval { get; set; } = TimeSpan.FromDays(1);

    /// <summary>
    /// Trailing window used to compute the "delivered (last interval)" throughput KPI
    /// surfaced on the Health dashboard and the Notification Outbox page.
    /// Default: 1 minute.
    /// </summary>
    public TimeSpan DeliveredKpiWindow { get; set; } = TimeSpan.FromMinutes(1);

    /// <summary>
    /// Maximum number of notifications delivered concurrently within a single dispatch
    /// sweep. Delivery is I/O-bound (SMTP / Twilio round trips of seconds each), so a
    /// purely sequential sweep caps throughput at ~1/attempt-latency regardless of batch
    /// size — an alarm storm queued behind one slow SMTP host would drain far too slowly
    /// (arch-review 04). Each concurrent delivery runs on its own DI scope/repository, so
    /// there is no shared-DbContext contention. Set to <c>1</c> to preserve the strictly
    /// sequential behaviour. Clamped to a minimum of 1 via <see cref="ResolvedMaxParallelDeliveries"/>.
    /// Default: 4.
    /// </summary>
    public int MaxParallelDeliveries { get; set; } = 4;

    /// <summary>
    /// Resolves the effective delivery parallelism, clamped to at least 1 so a
    /// misconfigured <c>0</c>/negative value cannot stall the dispatcher (a zero-count
    /// semaphore would deadlock the sweep).
    /// </summary>
    public int ResolvedMaxParallelDeliveries => MaxParallelDeliveries < 1 ? 1 : MaxParallelDeliveries;
}
