<?php

declare(strict_types=1);

namespace App\Framework\Discovery\Memory;

/**
 * Result of a guard check
 */
final readonly class GuardResult
{
    public function __construct(
        public MemoryStatusInfo $memoryStatus,
        public array            $actions,
        public int              $checkNumber,
        public bool             $emergencyMode
    ) {
    }

    public function hasAction(GuardAction $action): bool
    {
        return in_array($action, $this->actions, true);
    }

    public function hasAnyLeakDetection(): bool
    {
        return $this->hasAction(GuardAction::CRITICAL_LEAK_DETECTED) ||
            $this->hasAction(GuardAction::HIGH_LEAK_DETECTED) ||
            $this->hasAction(GuardAction::MEDIUM_LEAK_DETECTED) ||
            $this->hasAction(GuardAction::LOW_LEAK_DETECTED);
    }

    public function requiresImmediateAction(): bool
    {
        return $this->hasAction(GuardAction::EMERGENCY_CLEANUP) ||
            $this->hasAction(GuardAction::CRITICAL_LEAK_DETECTED);
    }

    public function toArray(): array
    {
        return [
            'memory_status' => $this->memoryStatus->toArray(),
            'actions' => array_map(fn ($action) => $action->value, $this->actions),
            'check_number' => $this->checkNumber,
            'emergency_mode' => $this->emergencyMode,
        ];
    }
}
