<?php

namespace App\Repository\Campaign\Report;

use App\Entity\Campaign\Report;
use App\Service\Campaign\ReportRepositoryInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Collections\Criteria;
use Symfony\Bridge\Doctrine\RegistryInterface;

final class DoctrineReportRepository extends ServiceEntityRepository implements ReportRepositoryInterface
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Report::class);
    }

    public function findById(int $id): ?Report
    {
        return $this->findOneBy(['id' => $id]);
    }

    /**
     * @param Report $report
     *
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function save(Report $report): void
    {
        $this->_em->persist($report);
        $this->_em->flush();
    }

    /**
     * @param Report $report
     *
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function delete(Report $report): void
    {
        $this->_em->remove($report);
        $this->_em->flush();
    }

    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
        $result = $this->queryByGrid($criteria, $orderBy, $limit, $offset);

        return $result->toArray();
    }

    public function count(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
        $result = $this->queryByGrid($criteria);

        return $result->count();
    }

    /**
     * @param array|null $orderBy
     * @param int|null $limit
     * @param int|null $offset
     */
    private function queryByGrid(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): \Doctrine\Common\Collections\Collection
    {
        $newCriteria = Criteria::create()
            ->setFirstResult($offset)
            ->setMaxResults($limit);

        if ($orderBy) {
            $newCriteria->orderBy($orderBy);
        }

        if (array_key_exists('q', $criteria)) {
            $newCriteria->where(Criteria::expr()->contains('title', $criteria['q']))
                ->orWhere(Criteria::expr()->contains('description', $criteria['q']));
        }

        if (array_key_exists('campaign', $criteria)) {
            $newCriteria->andWhere(Criteria::expr()->in('campaign', $criteria['campaign']));
        }

        if (array_key_exists('createdBy', $criteria)) {
            $newCriteria->andWhere(Criteria::expr()->in('createdBy', $criteria['createdBy']));
        }

        if (array_key_exists('user', $criteria)) {
            $newCriteria->andWhere(Criteria::expr()->in('user', $criteria['user']));
        }

        if (array_key_exists('conclusion', $criteria)) {
            $newCriteria->andWhere(Criteria::expr()->in('conclusion', $criteria['conclusion']));
        }

        if (array_key_exists('type', $criteria)) {
            $newCriteria->andWhere(Criteria::expr()->eq('type', $criteria['type']));
        }

        if (array_key_exists('clientAvailable', $criteria) && is_bool($criteria['clientAvailable'])) {
            $newCriteria->andWhere(Criteria::expr()->eq('clientAvailable', $criteria['clientAvailable']));
        }

        return $this->matching($newCriteria);
    }

    public function moveReportsBetweenUsers(int $from, int $to)
    {
        $qb = $this->createQueryBuilder('r');
        $qb
            ->update()
            ->set('r.user', $to)
            ->where('r.user = :from');

        $qb->setParameter('from', $from);

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