<?php

namespace Lc\ShopBundle\Repository;

use Lc\ShopBundle\Context\DefaultRepositoryInterface;
use Lc\ShopBundle\Context\ProductInterface;
use Lc\ShopBundle\Context\ReminderInterface;

/**
 * @method ReminderInterface|null find($id, $lockMode = null, $lockVersion = null)
 * @method ReminderInterface|null findOneBy(array $criteria, array $orderBy = null)
 * @method ReminderInterface[]    findAll()
 * @method ReminderInterface[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ReminderRepository extends BaseRepository implements DefaultRepositoryInterface
{
        public function getInterfaceClass()
        {
                return ReminderInterface::class;
        }


        public function findByUser($user)
        {
                $qb = $this->findByMerchantQuery()
                        ->leftJoin('e.users', 'u')
                        ->having('COUNT(u.id) = 0')
                        ->orHaving(':user MEMBER OF e.users')
                        ->andWhere('e.done = 0')
                        ->setParameter('user', $user)
                        ->orderBy('e.dateReminder', 'ASC')
                        ->groupBy('e.id');

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

        public function findByEasyAdminConfigAndUser($action, $entity, $user, $id = null)
        {
                $qb = $this->findByMerchantQuery();
                $qb->leftJoin('e.users', 'u');
                $qb->having('COUNT(u.id) = 0');
                $qb->orHaving(':user MEMBER OF e.users');
                $qb->andWhere('e.done = 0');
                $qb->andWhere('e.entityAction LIKE :action');
                $qb->andWhere('e.entityName LIKE :entity');
                $qb->setParameter('entity', $entity);
                $qb->setParameter('action', $action);
                $qb->setParameter('user', $user);
                if ($id) {
                        $qb->andWhere('e.entityId LIKE :id');
                        $qb->setParameter('id', $id);
                }
                $qb->orderBy('e.dateReminder', 'ASC');
                $qb->groupBy('e.id');
                return $qb->getQuery()->getResult();
        }

}
