<?php
// src/Repository/SystemPromptRepository.php

declare(strict_types=1);

namespace App\Repository;

use App\Entity\SystemPrompt;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class SystemPromptRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, SystemPrompt::class);
    }

    public function findActive(): ?SystemPrompt
    {
        return $this->createQueryBuilder('p')
            ->andWhere('p.active = true')
            ->orderBy('p.version', 'DESC')
            ->setMaxResults(1)
            ->getQuery()
            ->getOneOrNullResult();
    }

    public function getNextVersion(): int
    {
        $max = $this->createQueryBuilder('p')
            ->select('MAX(p.version)')
            ->getQuery()
            ->getSingleScalarResult();

        return ((int)$max) + 1;
    }
}
