<?php
declare(strict_types=1);

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

final class DistributedSchedulePolicy
{
    public const LEAST_LOADED = 'least_loaded';
    public const HIGHEST_CAPACITY = 'highest_capacity';
    public const ROUND_ROBIN = 'round_robin';

    public function __construct(
        private readonly string $strategy = self::LEAST_LOADED,
        private readonly int $defaultLeaseTtlSeconds = 60
    ) {}

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

    public function strategy(): string
    {
        return in_array($this->strategy, [self::LEAST_LOADED, self::HIGHEST_CAPACITY, self::ROUND_ROBIN], true)
            ? $this->strategy
            : self::LEAST_LOADED;
    }

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

    public function toArray(): array
    {
        return [
            'strategy' => $this->strategy(),
            'defaultLeaseTtlSeconds' => $this->defaultLeaseTtlSeconds(),
        ];
    }
}
