<?php
declare(strict_types=1);

namespace App\Service\Access;

use App\ApiResources\Access\AdminAccess;
use App\Entity\Organization\Organization;
use App\Service\Core\ContactPointUtils;
use App\Test\Service\Access\AdminAccessUtilsTest;

/**
 * Class AdminAccessUtils : Service contenant les manipulations associés à la ressource AdminAccess
 * @package App\Service\Access
 */
class AdminAccessUtils
{
    public function __construct(
        private Utils $accessUtils,
        private ContactPointUtils $contactPointUtils
    )
    { }

    /**
     * Renvoie l'objet AdminAccess initialisé par rapport à l'organization passée en paramètre
     *
     * @param Organization $organization
     * @return AdminAccess|null
     * @throws \Exception
     * @see AdminAccessUtilsTest::testGetAdminAccess()
     */
    public function getAdminAccess(Organization $organization): ?AdminAccess
    {
        $administrator = $this->accessUtils->findAdminFor($organization);
        if(!$administrator) return null;

        $contactPoint = $this->contactPointUtils->getPersonContactPointPrincipal($administrator);
        if(!$contactPoint) return null;

        $adminAccess = new AdminAccess();
        $adminAccess
            ->setId($administrator->getId())
            ->setUsername($administrator->getPerson()->getUserIdentifier())
            ->setEmail($contactPoint->getEmail())
        ;
        return $adminAccess;
    }
}
