<?php

declare(strict_types=1);

namespace App\Config;

/**
 * YAML-backed context configuration.
 *
 * This class intentionally has no PHP fallback values. Missing or invalid
 * configuration must be fixed in config/retriex/*.yaml instead of being hidden
 * by application defaults.
 */
final class ContextServiceConfig
{
    /**
     * @param array<string, mixed> $config
     */
    public function __construct(private readonly array $config)
    {
    }

    public function getMaxVisibleRegularLines(): int
    {
        return $this->requiredPositiveInt('max_visible_regular_lines');
    }

    public function getMaxFullLines(): int
    {
        return $this->requiredPositiveInt('max_full_lines');
    }

    private function requiredPositiveInt(string $key): int
    {
        if (!array_key_exists($key, $this->config)) {
            throw new \InvalidArgumentException(sprintf('Missing required RetrieX context config key "%s".', $key));
        }

        $value = $this->config[$key];

        if (is_int($value)) {
            $intValue = $value;
        } elseif (is_string($value) && preg_match('/^-?\d+$/', trim($value)) === 1) {
            $intValue = (int) trim($value);
        } else {
            throw new \InvalidArgumentException(sprintf('RetrieX context config key "%s" must be an integer.', $key));
        }

        if ($intValue <= 0) {
            throw new \InvalidArgumentException(sprintf('RetrieX context config key "%s" must be greater than 0.', $key));
        }

        return $intValue;
    }
}
