/**
 * Retry policy: pure scheduling rules, no I/O.
 *
 * P1-11: original policy was [1s, 5s, 30s] x 3 attempts = ~36s total before
 * permanent failure. A 1-minute Slack 502 would burn through retries and
 * silently mark the row failed. The new policy retries across two hours
 * (5s, 30s, 5m, 30m, 2h) so a real transient outage survives, while a
 * dead webhook still gives up in finite time and triggers the
 * notification_channel_failed meta-alert.
 *
 * `attempts` is the count AFTER the attempt that just happened. So a row
 * with attempts=1 has been tried once; nextDelaySec(1) tells you how long
 * to wait before the second try.
 */

export const RETRY_DELAYS_SECONDS = [
    5,
    30,
    5 * 60,
    30 * 60,
    2 * 60 * 60,
] as const;
export const DEFAULT_MAX_ATTEMPTS = 5;

export function nextDelaySec(attempts: number): number {
    const idx = Math.max(0, attempts - 1);
    if (idx >= RETRY_DELAYS_SECONDS.length) {
        return RETRY_DELAYS_SECONDS[RETRY_DELAYS_SECONDS.length - 1];
    }
    return RETRY_DELAYS_SECONDS[idx];
}

export function shouldGiveUp(attempts: number, maxAttempts: number = DEFAULT_MAX_ATTEMPTS): boolean {
    return attempts >= maxAttempts;
}

export function nextAttemptAt(now: Date, attempts: number): Date {
    return new Date(now.getTime() + nextDelaySec(attempts) * 1000);
}
