<?php

declare(strict_types=1);

namespace WPStack\Sandbox\Domain;

final class LaunchPolicy
{
    public const MAX_ACTIVE = 1;
    public const MAX_LAUNCHES_PER_HOUR = 5;
    public const MAX_GLOBAL_ACTIVE = 20;

    public function canLaunch(
        int $activeSessions,
        int $recentLaunches,
        int $globalActive = 0
    ): bool
    {
        return $this->denialReason($activeSessions, $recentLaunches, $globalActive) === null;
    }

    public function denialReason(
        int $activeSessions,
        int $recentLaunches,
        int $globalActive = 0
    ): ?string {
        if ($activeSessions >= self::MAX_ACTIVE) {
            return 'active_session';
        }

        if ($recentLaunches >= self::MAX_LAUNCHES_PER_HOUR) {
            return 'rate_limit';
        }

        if ($globalActive >= self::MAX_GLOBAL_ACTIVE) {
            return 'capacity';
        }

        return null;
    }
}
