<?php

namespace App\Repository\Campaign;

use App\Entity\Campaign;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Symfony\Bridge\Doctrine\RegistryInterface;

final class DoctrineCampaignRepository extends ServiceEntityRepository implements CampaignRepositoryInterface
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Campaign::class);
    }

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

    /**
     * @param Campaign $campaign
     *
     * @throws ORMException
     * @throws OptimisticLockException
     */
    public function save(Campaign $campaign): void
    {
        $this->_em->persist($campaign);
        $this->_em->flush();
    }

    /**
     * @param Campaign $campaign
     *
     * @throws ORMException
     * @throws OptimisticLockException
     */
    public function delete(Campaign $campaign): void
    {
        $this->_em->remove($campaign);
        $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();
    }

    /**
     * @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): 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('id', $criteria)) {
            $newCriteria->andWhere(Criteria::expr()->in('id', $criteria['id']));
        }

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

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

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

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

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

        if (array_key_exists('googleAnalyticsProfileId', $criteria) && is_null($criteria['googleAnalyticsProfileId'])) {
            $newCriteria->andWhere(Criteria::expr()->neq('googleAnalyticsProfileId', null));
        }

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

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

        return $result->count();
    }
}
