<?php

namespace App\Tag\Repository;

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

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

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

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

    public function save(TenantTag $tag): void
    {
        $this->getEntityManager()->persist($tag);
        $this->getEntityManager()->flush();
    }

    public function remove(TenantTag $tag): void
    {
        $this->getEntityManager()->remove($tag);
        $this->getEntityManager()->flush();
    }
}
