<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\CalendarBundle\Repository;

use Chill\CalendarBundle\Entity\Invite;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository;

class InviteRepository implements ObjectRepository
{
    private readonly EntityRepository $entityRepository;

    public function __construct(EntityManagerInterface $em)
    {
        $this->entityRepository = $em->getRepository(Invite::class);
    }

    public function find($id): ?Invite
    {
        return $this->entityRepository->find($id);
    }

    /**
     * @return array|Invite[]
     */
    public function findAll(): array
    {
        return $this->entityRepository->findAll();
    }

    /**
     * @return array|Invite[]
     */
    public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
    {
        return $this->entityRepository->findBy($criteria, $orderBy, $limit, $offset);
    }

    public function findOneBy(array $criteria): ?Invite
    {
        return $this->entityRepository->findOneBy($criteria);
    }

    /**
     * Find accepted invites for a user within a date range.
     *
     * @return array|Invite[]
     */
    public function findAcceptedInvitesByUserAndDateRange(User $user, \DateTimeImmutable $from, \DateTimeImmutable $to): array
    {
        return $this->buildAcceptedInviteByUserAndDateRangeQuery($user, $from, $to)
            ->getQuery()
            ->getResult();
    }

    /**
     * Count accepted invites for a user within a date range.
     */
    public function countAcceptedInvitesByUserAndDateRange(User $user, \DateTimeImmutable $from, \DateTimeImmutable $to): int
    {
        return $this->buildAcceptedInviteByUserAndDateRangeQuery($user, $from, $to)
            ->select('COUNT(c)')
            ->getQuery()
            ->getSingleScalarResult();
    }

    public function countPendingInvitesByUser(User $user): int
    {
        $qb = $this->entityRepository->createQueryBuilder('i');

        $qb->select('COUNT(i)')
            ->where(
                $qb->expr()->andX(
                    $qb->expr()->eq('i.user', ':user'),
                    $qb->expr()->eq('i.status', ':status')
                )
            )
            ->setParameters([
                'user' => $user,
                'status' => Invite::PENDING,
            ]);

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

    public function buildAcceptedInviteByUserAndDateRangeQuery(User $user, \DateTimeImmutable $from, \DateTimeImmutable $to)
    {
        $qb = $this->entityRepository->createQueryBuilder('i');

        return $qb
            ->join('i.calendar', 'c')
            ->where(
                $qb->expr()->andX(
                    $qb->expr()->eq('i.user', ':user'),
                    $qb->expr()->eq('i.status', ':status'),
                    $qb->expr()->gte('c.startDate', ':startDate'),
                    $qb->expr()->lte('c.endDate', ':endDate'),
                    $qb->expr()->isNull('c.cancelReason')
                )
            )
            ->setParameters([
                'user' => $user,
                'status' => Invite::ACCEPTED,
                'startDate' => $from,
                'endDate' => $to,
            ]);
    }

    public function getClassName(): string
    {
        return Invite::class;
    }
}
