<?php

declare(strict_types=1);

namespace App\Service;

use App\Entity\ModelGenerationConfig;
use App\Repository\ModelGenerationConfigRepository;

final readonly class ModelGenerationConfigProvider
{
    public function __construct(
        private ModelGenerationConfigRepository $repository
    )
    {
    }

    public function getActiveForModel(): ModelGenerationConfig
    {
        $config = $this->repository->findActiveForModel();

        if ($config !== null) {
            return $config;
        }

        // ------------------------------
        // Safe Enterprise Default Fallback
        // ------------------------------
        return new ModelGenerationConfig(
            modelName: 'mto-model',
            version: 0,
            stream: false,
            temperature: 0.1,
            topK: 20,
            topP: 0.8,
            repeatPenalty: 1.05,
            numCtx: 4096,
            active: false
        );
    }

    public function getActiveNumCtx(): int
    {
        $numCtx = (int) $this->getActiveForModel()->getNumCtx();

        return max(512, $numCtx);
    }
}