<?php
declare(strict_types=1);

namespace App\Repository\Core;

use App\Entity\Core\ContactPoint;
use App\Entity\Organization\Organization;
use App\Entity\Person\Person;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @method ContactPoint|null find($id, $lockMode = null, $lockVersion = null)
 * @method ContactPoint|null findOneBy(array $criteria, array $orderBy = null)
 * @method ContactPoint[]    findAll()
 * @method ContactPoint[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ContactPointRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, ContactPoint::class);
    }

    /**
     * Récupération des points de contacts d'une organization et d'un type précis
     * @param String $type
     * @param Organization $organization
     * @return list<ContactPoint>
     */
    public function getByTypeAndOrganization(String $type, Organization $organization): array
    {
        return $this->createQueryBuilder('contact_point')
            ->innerJoin('contact_point.organization', 'organization')
            ->where('contact_point.contactType = :type')
            ->andWhere('organization.id = :organizationId')
            ->setParameter('type', $type)
            ->setParameter('organizationId', $organization->getId())
            ->getQuery()
            ->getResult()
            ;
    }

    /**
     * Récupération des points de contacts d'une person et d'un type précis
     * @param String $type
     * @param Person $person
     * @return list<ContactPoint>
     */
    public function getByTypeAndPerson(String $type, Person $person): array
    {
        return $this->createQueryBuilder('contact_point')
            ->innerJoin('contact_point.person', 'person')
            ->where('contact_point.contactType = :type')
            ->andWhere('person.id = :personId')
            ->setParameter('type', $type)
            ->setParameter('personId', $person->getId())
            ->getQuery()
            ->getResult()
            ;
    }

}
