<?php
declare(strict_types=1);

namespace App\Repository\Access;

use App\DQL\DateConditions;
use App\Entity\Access\Access;
use App\Entity\Organization\Organization;
use DateTime;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * @method Access|null find($id, $lockMode = null, $lockVersion = null)
 * @method Access|null findOneBy(array $criteria, array $orderBy = null)
 * @method Access[]    findAll()
 * @method Access[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class AccessRepository extends ServiceEntityRepository implements UserLoaderInterface
{
    const ACCESS_NAME_HEADER = 'X-AccessId';
    const HTTP_X_SWITCH_USER = 'X-Switch-User';

    public function __construct(ManagerRegistry $registry, private RequestStack $requestStack)
    {
        parent::__construct($registry, Access::class);
    }

    /**
     * @deprecated  in symfony 6.0, replace by loadUserByIdentifier
     * @param string $username
     * @return \Symfony\Component\Security\Core\User\UserInterface|void|null
     */
    public function loadUserByUsername(string $username)
    {}

    /**
     * Méthode permettant de fournir un userProvider custom (voir config provider : access_provider)
     * @param string $username
     * @return Access|mixed|object|\Symfony\Component\Security\Core\User\UserInterface|null
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     * @throws \Doctrine\ORM\TransactionRequiredException
     */
    public function loadUserByIdentifier($identifier): mixed
    {
        if($this->requestStack->getMainRequest()->headers->get(self::HTTP_X_SWITCH_USER) == $identifier)
            return $this->getEntityManager()->find(Access::class, $identifier);

        return $this->findAccessByUsernameAndAccessId($identifier, $this->requestStack->getMainRequest()->headers->get(self::ACCESS_NAME_HEADER));
    }

    /**
     * Récupère un access grâce à son username et son ID
     * @param string $username
     * @param $id
     * @return mixed
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    private function findAccessByUsernameAndAccessId(string $username, $id): mixed
    {
        $entityManager = $this->getEntityManager();
        return $entityManager->createQuery(
            'SELECT a
                FROM App\Entity\Access\Access a
                INNER JOIN a.person p
                WHERE p.username = :query AND a.id = :id'
        )
            ->setParameter('query', $username)
            ->setParameter('id', $id)
            ->getOneOrNullResult();
    }

    /**
     * @param Access $access
     * @return mixed
     * @throws \Exception
     */
    public function findAllValidAccesses(Access $access): array
    {
        $datetime = new \DateTime();
        $today = $datetime->format('Y-m-d');

        return $this->createQueryBuilder('access')
            ->innerJoin('access.organization', 'organization')
            ->innerJoin('organization.networkOrganizations', 'networkOrganizations')
            ->where('access.person = :person')
            ->andWhere('networkOrganizations.startDate <= :today')
            ->setParameter('person', $access->getPerson())
            ->setParameter('today', $today)
            ->getQuery()
            ->getResult()
      ;
    }

    public function hasGotFunctionAtThisDate(Access $access, $function, \DateTime $date): bool
    {
        $this->_em->getFilters()->disable('date_time_filter');

        $qb = $this->createQueryBuilder('access');
        $qb
            ->innerJoin('access.organizationFunction', 'organization_function')
            ->innerJoin('organization_function.functionType', 'function_type')
            ->where('function_type.mission = :mission')
            ->andWhere('access.id = :id')
            ->setParameter('id', $access->getId())
            ->setParameter('mission', $function)
        ;
        DateConditions::addDateInPeriodCondition($qb, 'organization_function', $date->format('Y-m-d'));

        $result = count($qb->getQuery()->getResult()) > 0;

        $this->_em->getFilters()->enable('date_time_filter');

        return $result;
    }

    /**
     * Retourne tous les accesses de l'organization ayant la fonction donnée à la date donnée
     *
     * @param Organization $organization
     * @param $function
     * @param DateTime|null $date
     * @return array
     */
    public function findByOrganizationAndMission(Organization $organization, $function, \DateTime $date = null): array
    {
        if ($date === null)
            $date = new DateTime();

        $this->_em->getFilters()->disable('date_time_filter');

        $qb = $this->createQueryBuilder('access');
        $qb
            ->innerJoin('access.organizationFunction', 'organization_function')
            ->innerJoin('organization_function.functionType', 'function_type')
            ->where('function_type.mission = :mission')
            ->andWhere('access.organization = :id')
            ->setParameter('id', $organization->getId())
            ->setParameter('mission', $function)
        ;
        DateConditions::addDateInPeriodCondition($qb, 'organization_function', $date->format('Y-m-d'));
        $this->_em->getFilters()->enable('date_time_filter');

        return $qb->getQuery()->getResult();
    }

    /**
     * Get all the currently active accesses and return an array
     * of the form ['id' => $accessId, 'organization_id' => $organizationId, 'mission' => $mission]
     *
     * Used by App\Service\Dolibarr\DolibarrSync\DolibarrSyncService
     *
     * @return array
     */
    public function getAllActiveMembersAndMissions(): array
    {
        $qb = $this->createQueryBuilder('access');
        $qb
            ->select('access.id', 'organization.id as organization_id', 'function_type.mission')
            ->innerJoin('access.organization', 'organization')
            ->innerJoin('access.organizationFunction', 'organization_function')
            ->innerJoin('organization_function.functionType', 'function_type')
        ;
        DateConditions::addDateInPeriodCondition($qb, 'organization_function', date('Y-m-d'));

        return $qb->getQuery()->getArrayResult();
    }
}
