<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Retry\Policy;

use PpPhp\Core\Platform\Event\Retry\Contracts\RetryPolicyInterface;
use PpPhp\Core\Platform\Event\Retry\RetryContext;

final class ExponentialBackoffPolicy implements RetryPolicyInterface
{
    public function __construct(private readonly int $baseSeconds = 2, private readonly int $maxSeconds = 300) {}
    public function name(): string { return 'exponential-backoff'; }
    public function shouldRetry(RetryContext $context): bool { return $context->attempt() < $context->maxAttempts(); }
    public function nextDelaySeconds(RetryContext $context): int
    {
        return min($this->maxSeconds, $this->baseSeconds ** max(1, $context->attempt()));
    }
}
