<?php

namespace App\Repository\NegotiationMessage;

use App\Entity\NegotiationMessage;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Collections\Criteria;
use Symfony\Bridge\Doctrine\RegistryInterface;

final class DoctrineNegotiationMessageRepository extends ServiceEntityRepository implements NegotiationMessageRepositoryInterface
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, NegotiationMessage::class);
    }

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

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

    /**
     * @param NegotiationMessage $negotiationMessage
     *
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function delete(NegotiationMessage $negotiationMessage): void
    {
        $this->_em->remove($negotiationMessage);
        $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('description', $criteria['q']));
        }

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

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

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

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

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

    public function findAll()
    {
        return $this->findBy([], ['createdAt' => 'DESC']);
    }

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

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

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