<?php

declare(strict_types=1);

namespace App\Repository;

use App\Entity\IngestProfile;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Uid\Uuid;

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

    public function findLatestVersion(): ?IngestProfile
    {
        return $this->createQueryBuilder('p')
            ->orderBy('p.version', 'DESC')
            ->setMaxResults(1)
            ->getQuery()
            ->getOneOrNullResult();
    }

    public function findActive(): ?IngestProfile
    {
        return $this->findOneBy(['active' => true]);
    }

    public function remove(string $id): void
    {
        $entity = $this->find($id);

        if (!$entity instanceof IngestProfile) {
            return;
        }

        $em = $this->getEntityManager();
        $em->remove($entity);
        $em->flush();
    }
}
