<?php

namespace App\Resource\Repository;

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

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

    public function findByUuid(string $uuid): ?ResourceType
    {
        return $this->findOneBy(['uuid' => $uuid]);
    }

    /** @return ResourceType[] */
    public function findForPair(string $entityType, int $entityId): array
    {
        return $this->createQueryBuilder('t')
            ->where('t.entityType = :type')
            ->andWhere('t.entityId = :id')
            ->setParameter('type', $entityType)
            ->setParameter('id', $entityId)
            ->orderBy('t.isSystem', 'DESC')
            ->addOrderBy('t.name', 'ASC')
            ->getQuery()
            ->getResult();
    }

    public function findByCode(string $entityType, int $entityId, string $code): ?ResourceType
    {
        return $this->findOneBy(['entityType' => $entityType, 'entityId' => $entityId, 'code' => $code]);
    }
}
