<?php

namespace Lc\ShopBundle\Repository;

use Lc\ShopBundle\Context\DefaultRepositoryInterface;
use Lc\ShopBundle\Context\OrderShopInterface;

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

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


        protected $statusAliasAsValid = array('paid-online', 'paid-by-credit', 'waiting-delivery', 'waiting-delivery-with-payment', 'delivered-without-payment', 'done');


        public function getValidOrderWithReductionCredit($reductionCredit, $user){
                $query = $this->findValidOrderQuery();
                $query->andWhere('e.user = :user');
                $query->leftJoin('e.orderReductionCredits', 'orc');
                $query->andWhere('orc.reductionCredit = :reductionCredit');
                $query->setParameter('reductionCredit', $reductionCredit);
                $query->setParameter('user', $user);
                return $query->getQuery()->getResult();
        }


        public function findValidOrderQuery(){
                $query = $this->findByMerchantQuery();
                $query->leftJoin('e.orderStatus', 'os');
                $query->andWhere('os.alias IN (:alias)');
                $query->setParameter('alias', $this->statusAliasAsValid);

                return $query;
        }

        public function findCartCurrent($params)
        {
                $query = $this->findByMerchantQuery() ;

                if(isset($params['user'])) {
                        $query->andWhere('e.user = :user')->setParameter('user', $params['user']) ;
                }

                if(isset($params['visitor'])) {
                        $query->andWhere('e.visitor = :visitor')->setParameter('visitor', $params['visitor']) ;
                }

                $this->filterOrderCart($query, true) ;

                $results = $query->getQuery()->getResult()  ;

                if($results) {
                        return $results[0]  ;
                }
                
                return null ;
        }

        public function findAllBy($params = [])
        {
                $query = $this->findByMerchantQuery() ;

                if(isset($params['dateStart'])) {
                        $query->andWhere('e.createdAt >= :dateStart')->setParameter('dateStart', $params['dateStart']) ;
                }

                if(isset($params['dateEnd'])) {
                        $query->andWhere('e.createdAt <= :dateEnd')->setParameter('dateEnd', $params['dateEnd']) ;
                }

                if(!isset($params['isCart'])) {
                        $this->filterOrderCart($query) ;
                }

                if(isset($params['user'])) {
                        $query->andWhere('e.user = :user')->setParameter('user', $params['user']) ;
                }

                $query->orderBy('e.id', 'DESC') ;

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

        public function findAllByDateStartEnd($dateStart, $dateEnd)
        {
                $query = $this->findByMerchantQuery()
                        ->andWhere('e.createdAt >= :dateStart')
                        ->setParameter('dateStart', $dateStart)
                        ->andWhere('e.createdAt <= :dateEnd')
                        ->setParameter('dateEnd', $dateEnd);

                $this->filterOrderCart($query) ;

                $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone') ;
                $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone') ;
                $query->addOrderBy('deliverySlotZone.day', 'ASC') ;
                $query->addOrderBy('deliverySlotZone.timeStart', 'ASC') ;

                $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale') ;
                $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale') ;
                $query->addOrderBy('deliverySlotPointSale.day', 'ASC') ;
                $query->addOrderBy('deliverySlotPointSale.timeStart', 'ASC') ;

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

        public function filterOrderCart($query, $isCart = false)
        {
                $operator = $isCart ? '=' : '!=' ;
                return $query->leftJoin('e.orderStatus', 'orderStatus')
                        ->andWhere('orderStatus.alias '.$operator.' :orderStatus')
                        ->setParameter('orderStatus', 'cart');
        }

              //getValidOrders()
}
