<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Streaming\Dlq;

final class StreamRetryPolicy
{
    public function __construct(
        private readonly int $maxAttempts = 3,
        private readonly int $baseDelaySeconds = 5,
        private readonly float $backoffMultiplier = 2.0
    ) {}

    public static function default(): self
    {
        return new self();
    }

    public function maxAttempts(): int
    {
        return max(1, $this->maxAttempts);
    }

    public function delayForAttempt(int $attempt): int
    {
        $attempt = max(1, $attempt);
        return (int) round(max(0, $this->baseDelaySeconds) * ($this->backoffMultiplier ** ($attempt - 1)));
    }

    public function toArray(): array
    {
        return [
            'maxAttempts' => $this->maxAttempts(),
            'baseDelaySeconds' => max(0, $this->baseDelaySeconds),
            'backoffMultiplier' => $this->backoffMultiplier,
        ];
    }
}
