<?php

namespace App\Repository;

use App\Entity\Bibliography;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Query\ResultSetMappingBuilder;

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

    public function hasCreatorEditor(string $creator): bool
    {
        $em = $this->getEntityManager();
        $repo = $em->getRepository(User::class);
        
        $creator = $repo->findOneBy(['username' => $creator]);

        return in_array('ROLE_EDITOR', $creator->getRoles());
    }

    public function findAllByCollection(int $collectionId): ?ArrayCollection
    {
        $rsm = new ResultSetMappingBuilder($this->getEntityManager());
        $rsm->addRootEntityFromClassMetadata('App\Entity\Bibliography', 'b');

        $query = $this->getEntityManager()->createNativeQuery(
            "SELECT id, stato, editor, cit_bib, rif_bib FROM bibliography b
            JOIN rel_riferimento_collezione
            ON Bibliografia_id_bib = id
            WHERE Collezione_id_coll = :collId",
            $rsm
        );
        $query->setParameter('collId', $collectionId);

        $bibliographies = new ArrayCollection($query->getResult());

        return $bibliographies;
    }

    public function findAllByCollector(int $collectorId): ?ArrayCollection
    {
        $rsm = new ResultSetMappingBuilder($this->getEntityManager());
        $rsm->addRootEntityFromClassMetadata('App\Entity\Bibliography', 'b');

        $query = $this->getEntityManager()->createNativeQuery(
            "SELECT
                id,
                stato,
                editor,
                cit_bib,
                rif_bib
            FROM bibliography b
            JOIN rel_riferimento_personaggio
            ON Bibliografia_id_bib = id
            WHERE Personaggio_id_pers = :collId",
            $rsm
        );
        $query->setParameter('collId', $collectorId);

        $bibliographies = new ArrayCollection($query->getResult());

        return $bibliographies;
    }

    public function findAllByDocument(int $documentId): ?ArrayCollection
    {
        $rsm = new ResultSetMappingBuilder($this->getEntityManager());
        $rsm->addRootEntityFromClassMetadata('App\Entity\Bibliography', 'b');

        $query = $this->getEntityManager()->createNativeQuery(
            "SELECT
                id,
                stato,
                editor,
                cit_bib,
                rif_bib
            FROM bibliography b
            JOIN rel_riferimento_documento
            ON Bibliografia_id_bib = id
            WHERE Documento_id_doc = :docId",
            $rsm
        );
        $query->setParameter('docId', $documentId);

        $bibliographies = new ArrayCollection($query->getResult());

        return $bibliographies;
    }
}
