<?php

declare(strict_types=1);

namespace App\Service;

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

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

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

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

        return new ModelGenerationConfig(
            modelName: $this->defaults->getModelName(),
            version: 0,
            stream: $this->defaults->isStream(),
            temperature: $this->defaults->getTemperature(),
            topK: $this->defaults->getTopK(),
            topP: $this->defaults->getTopP(),
            repeatPenalty: $this->defaults->getRepeatPenalty(),
            numCtx: $this->defaults->getNumCtx(),
            active: false,
            retrievalMaxChunks: $this->defaults->getRetrievalMaxChunks(),
            retrievalVectorTopK: $this->defaults->getRetrievalVectorTopK(),
        );
    }

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

        return max(512, $numCtx);
    }
}
