<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Worker\Monitoring;

final class WorkerRecoveryPolicy
{
    public function __construct(
        private readonly bool $recoverStale = true,
        private readonly bool $recoverHung = true,
        private readonly bool $recoverOffline = true,
        private readonly bool $recoverFailed = true,
        private readonly int $maxRecoveriesPerRun = 50
    ) {}

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

    public function shouldRecover(string $classification): bool
    {
        return match ($classification) {
            WorkerStaleRecord::STALE => $this->recoverStale,
            WorkerStaleRecord::HUNG => $this->recoverHung,
            WorkerStaleRecord::OFFLINE => $this->recoverOffline,
            WorkerStaleRecord::FAILED => $this->recoverFailed,
            default => false,
        };
    }

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

    public function toArray(): array
    {
        return [
            'recoverStale' => $this->recoverStale,
            'recoverHung' => $this->recoverHung,
            'recoverOffline' => $this->recoverOffline,
            'recoverFailed' => $this->recoverFailed,
            'maxRecoveriesPerRun' => $this->maxRecoveriesPerRun(),
        ];
    }
}
