<?php
declare(strict_types=1);

namespace App\Service\Core;

use App\Entity\Access\Access;
use App\Entity\Core\ContactPoint;
use App\Entity\Organization\Organization;
use App\Enum\Core\ContactPointTypeEnum;
use App\Repository\Core\ContactPointRepository;
use App\Test\Service\Access\ContactPointUtilsTest;


/**
 * Classe ContactPointUtils qui possédant les fonctions utils à l'entité ContactPoint
 */
class ContactPointUtils
{
    public function __construct(private ContactPointRepository $contactPointRepository){
    }

    /**
     * Renvoie le point de contact principal de l'Access, ou null si aucun trouvé.
     *
     * @param Access $access
     * @return ContactPoint|null
     * @see ContactPointUtilsTest::testGetPersonContactPointPrincipal()
     */
    public function getPersonContactPointPrincipal(Access $access): ?ContactPoint
    {
        $contactPoints = $this->contactPointRepository->getByTypeAndPerson(
            ContactPointTypeEnum::PRINCIPAL()->getValue(),
            $access->getPerson()
        );

        if (count($contactPoints) === 0) {
            return null;
        }

        if (count($contactPoints) !== 1){
            throw new \RuntimeException('more_than_one_result');
        }
        return $contactPoints[0];
    }
}
