<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Distributed\Failover;

final class DistributedFailoverPolicy
{
    public function __construct(
        private readonly int $nodeTimeoutSeconds = 60,
        private readonly bool $recoverExpiredLeases = true,
        private readonly bool $rebalanceQueues = true,
        private readonly int $maxRecoveriesPerRun = 50
    ) {}

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

    public static function strict(): self
    {
        return new self(5, true, true, 50);
    }

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

    public function recoverExpiredLeases(): bool
    {
        return $this->recoverExpiredLeases;
    }

    public function rebalanceQueues(): bool
    {
        return $this->rebalanceQueues;
    }

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

    public function toArray(): array
    {
        return [
            'nodeTimeoutSeconds' => $this->nodeTimeoutSeconds(),
            'recoverExpiredLeases' => $this->recoverExpiredLeases,
            'rebalanceQueues' => $this->rebalanceQueues,
            'maxRecoveriesPerRun' => $this->maxRecoveriesPerRun(),
        ];
    }
}
