<?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 App\Enum\Core\ContactPointTypeEnum;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @method ContactPoint|null find($id, $lockMode = null, $lockVersion = null)
 * @method ContactPoint|null findOneBy(mixed[] $criteria, mixed[] $orderBy = null)
 * @method ContactPoint[]    findAll()
 * @method ContactPoint[]    findBy(mixed[] $criteria, mixed[] $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.
     *
     * @return list<ContactPoint>
     */
    public function getByTypeAndOrganization(ContactPointTypeEnum $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.
     *
     * @return list<ContactPoint>
     */
    public function getByTypeAndPerson(ContactPointTypeEnum $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()
        ;
    }
}
