<?php

namespace App\Repository;

use App\Entity\Image;
use App\Entity\ImageImageRepository;
use App\Entity\ImageRepository as Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<ImageImageRepositoryRepository>
 */
class ImageImageRepositoryRepository extends AbstractRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, ImageImageRepository::class);
    }

    public function findLatestVersionByImageAndRepository(Image $image, Repository $repository): ?ImageImageRepository
    {
        return $this->createQueryBuilder('i')
            ->andWhere('i.image = :imageId')
            ->setParameter('imageId', $image->getId())
            ->andWhere('i.repository = :repository')
            ->setParameter('repository', $repository->getId())
            ->orderBy('i.version', 'DESC')
            ->setMaxResults(1)
            ->getQuery()
            ->getOneOrNullResult();
    }

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

}
