<?php

namespace App\Modules\Integrations\Platform\Services;

/**
 * Exponential backoff for outbound webhook delivery attempts.
 */
final class WebhookRetryPolicy
{
    public const MAX_ATTEMPTS = 12;

    public static function maxAttempts(): int
    {
        return self::MAX_ATTEMPTS;
    }

    /**
     * Delay before the next queue attempt, based on completed attempts so far.
     */
    public static function delaySecondsAfterAttempt(int $attemptCount): int
    {
        $n = max(0, $attemptCount - 1);

        return min(3600, (int) round(10 * (2 ** min($n, 10))));
    }

    public static function isExhausted(int $attemptCount): bool
    {
        return $attemptCount >= self::MAX_ATTEMPTS;
    }
}
