<?php

declare(strict_types=1);

final class MyronSiteTypeStageGateService
{
    public function __construct(
        private readonly int $currentStage = 5,
    ) {
    }

    /**
     * @param array<string, mixed> $siteType
     */
    public function isVisible(array $siteType): bool
    {
        $stageGate = is_array($siteType['stageGate'] ?? null) ? $siteType['stageGate'] : [];
        $visibleFromStage = (int) ($stageGate['visibleFromStage'] ?? 0);

        return $visibleFromStage <= 0 || $this->currentStage >= $visibleFromStage;
    }

    /**
     * @param list<array<string, mixed>> $siteTypes
     * @return list<array<string, mixed>>
     */
    public function visibleSiteTypes(array $siteTypes): array
    {
        return array_values(array_filter($siteTypes, fn (array $siteType): bool => $this->isVisible($siteType)));
    }

    /**
     * @param array<string, mixed> $siteType
     */
    public function hiddenReason(array $siteType): string
    {
        $stageGate = is_array($siteType['stageGate'] ?? null) ? $siteType['stageGate'] : [];
        $visibleFromStage = (int) ($stageGate['visibleFromStage'] ?? 0);
        if ($visibleFromStage <= 0 || $this->isVisible($siteType)) {
            return '';
        }

        $rationale = trim((string) ($stageGate['rationale'] ?? ''));
        return $rationale !== ''
            ? $rationale
            : 'This site type is hidden until Stage ' . (string) $visibleFromStage . ' foundation support is available.';
    }
}
