<?php

declare(strict_types=1);

namespace App\Service;

use App\Entity\ModelGenerationConfig;
use App\Repository\ModelGenerationConfigRepository;
use Doctrine\ORM\EntityManagerInterface;

final class ModelGenerationConfigManager
{
    public function __construct(
        private EntityManagerInterface $em,
        private ModelGenerationConfigRepository $repository
    ) {}

    public function activate(ModelGenerationConfig $config): void
    {
        $this->validate($config);

        $this->em->wrapInTransaction(function () use ($config) {

            $activeConfigs = $this->repository->findBy([
                'active' => true
            ]);

            foreach ($activeConfigs as $active) {
                $active->setActive(false);
                $this->em->persist($active);
            }

            // Diese aktivieren
            $config->setActive(true);
            $this->em->persist($config);

            $this->em->flush();
        });
    }

    private function validate(ModelGenerationConfig $config): void
    {
        // Guardrails

        if ($config->getTemperature() < 0.0 || $config->getTemperature() > 2.0) {
            throw new \InvalidArgumentException('Temperature must be between 0.0 and 2.0');
        }

        if ($config->getTopP() <= 0.0 || $config->getTopP() > 1.0) {
            throw new \InvalidArgumentException('Top P must be between 0.0 and 1.0');
        }

        if ($config->getTopK() < 0) {
            throw new \InvalidArgumentException('Top K must be > 0');
        }

        if ($config->getRepeatPenalty() < 0.0 || $config->getRepeatPenalty() > 5.0) {
            throw new \InvalidArgumentException('Repeat Penalty out of allowed range');
        }

        if ($config->getNumCtx() < 512 || $config->getNumCtx() > 32768) {
            throw new \InvalidArgumentException('Num Ctx outside safe range');
        }

        // Enterprise RAG Warnbereich (optional, nur Logging)
        if ($config->getTemperature() > 0.7) {
            // hier könntest man optional Logging einbauen
        }
    }
}
