namespace ZB.MOM.WW.ScadaBridge.Commons.Types;

/// <summary>
/// Shared initial-delay rule for the central retention-purge singletons
/// (Audit Log partition purge, Site Call Audit terminal purge, KPI History
/// sample purge). All three run on a long (up to 24 h) interval; scheduling the
/// FIRST tick after a full interval meant a node that recycles more often than
/// the interval — e.g. a daily container restart against a 24 h cadence — would
/// never actually purge (arch-review 04, S9).
/// </summary>
/// <remarks>
/// Purges are idempotent, so an early first sweep is always safe. Taking
/// <c>min(interval, <see cref="ShortFirstTick"/>)</c> guarantees the first purge
/// runs within five minutes of startup while leaving the steady-state cadence —
/// and the millisecond intervals the actor test suites use — unchanged.
/// </remarks>
public static class PurgeTimerSchedule
{
    /// <summary>
    /// Upper bound on the delay before the first purge tick. Short enough that a
    /// frequently-recycled node still purges, long enough not to add startup churn.
    /// </summary>
    public static readonly TimeSpan ShortFirstTick = TimeSpan.FromMinutes(5);

    /// <summary>
    /// The initial delay a purge timer should use for its first tick given its
    /// steady-state <paramref name="interval"/>: the interval itself when it is
    /// already shorter than <see cref="ShortFirstTick"/> (preserving fast test
    /// cadences), otherwise capped at <see cref="ShortFirstTick"/>.
    /// </summary>
    /// <param name="interval">The purge timer's steady-state interval.</param>
    /// <returns>The initial delay to use before the first purge tick.</returns>
    public static TimeSpan InitialDelay(TimeSpan interval) =>
        interval < ShortFirstTick ? interval : ShortFirstTick;
}
