<?php

namespace Lc\ShopBundle\Repository;

use Doctrine\ORM\EntityManager;
use Lc\ShopBundle\Context\DefaultRepositoryInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\ReductionCreditInterface;
use Lc\ShopBundle\Model\ReductionCredit;

/**
 * @method ReductionCreditInterface|null find($id, $lockMode = null, $lockVersion = null)
 * @method ReductionCreditInterface|null findOneBy(array $criteria, array $orderBy = null)
 * @method ReductionCreditInterface[]    findAll()
 * @method ReductionCreditInterface[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ReductionCreditRepository extends BaseRepository implements DefaultRepositoryInterface
{

        public function getInterfaceClass()
        {
                return ReductionCreditInterface::class;
        }


        public function findReductionCreditsByUser($user)
        {
                $query = $this->findByMerchantQuery() ;
                $query->andWhere('e.status = 1');
                $query->andWhere('e.type = :type');
                $query->andWhere(':user MEMBER OF e.users');
                $query->setParameter('user', $user);
                $query->setParameter('type', ReductionCredit::TYPE_CREDIT);
                return $query->getQuery()->getResult() ;
        }

        public function findReductionGiftToUseByUser($user){
                $query = $this->findByMerchantQuery() ;
                $query->andWhere('e.status = 1');
                $query->andWhere('e.type = :type');
                $query->andWhere(':user MEMBER OF e.users');
                $query->andWhere(':now > e.activationDate');
                $query->setParameter('now', new \DateTime()) ;

                $query->setParameter('user', $user);
                $query->setParameter('type', ReductionCredit::TYPE_GIFT);
                return $query->getQuery()->getResult() ;
        }


        public function findReductionGiftOwnedByUser($user){
                $query = $this->findByMerchantQuery() ;
                $query->leftJoin('e.users', 'u');
                $query->having('COUNT(u.id) =0');
                $query->andWhere('e.status = 1');
                $query->andWhere('e.type = :type');
                $query->andWhere('e.owner = :user');
                $query->setParameter('user', $user);
                $query->setParameter('type', ReductionCredit::TYPE_GIFT);
                $query->groupBy('e.id');
                return $query->getQuery()->getResult() ;
        }

        public function findReductionGiftOwnedActiveByUser($user){
                $query = $this->findByMerchantQuery() ;
                $query->leftJoin('e.users', 'u');
                $query->having('COUNT(u.id) >0');
                $query->andWhere('e.status = 1');
                $query->andWhere('e.type = :type');
                $query->andWhere('e.owner = :user');
                $query->setParameter('user', $user);
                $query->setParameter('type', ReductionCredit::TYPE_GIFT);
                $query->groupBy('e.id');

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