<?php

namespace Lc\ShopBundle\Repository;

use Lc\ShopBundle\Context\DefaultRepositoryInterface;
use Lc\ShopBundle\Context\ReductionCatalogInterface;
use Symfony\Component\Validator\Constraints\DateTime;

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


        public function getReductionCatalogByProductFamily($productFamily, $user)
        {

                $query = $this->findByMerchantQuery();
                $query->andWhere('e.status = 1');
                $query->andWhere(':user MEMBER OF e.users OR e.users is empty');
                $query->andWhere(':groupUser MEMBER OF e.groupUsers OR e.groupUsers is empty');
                $query->andWhere(':productFamily MEMBER OF e.productFamilies OR e.productFamilies is empty');
                $query->andWhere(':productCategory MEMBER OF e.productCategories OR e.productCategories is empty');
                $query->andWhere(':supplier MEMBER OF e.suppliers OR e.suppliers is empty');
                $query->setParameter('user', $user);
                $query->setParameter('groupUser', $user->getGroupUsers());
                $query->setParameter('productFamily', $productFamily);
                $query->setParameter('productCategory', $productFamily->getProductCategories());
                $query->setParameter('supplier', $productFamily->getSupplier());


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


        }


        public function getReductionCatalogByProductFamilyConditions($productFamilyIds, $user)
        {
                dump($user);

                $query = $this->findByMerchantQuery();
                $query->andWhere('e.status = 1');
                $query->andWhere('e.permanent = 1 OR (e.dateStart <= :now AND e.dateEnd >= :now)');
                if($user){
                        $query->andWhere(':user MEMBER OF e.users OR e.users is empty');
                        $query->andWhere(':groupUser MEMBER OF e.groupUsers OR e.groupUsers is empty');
                        $query->setParameter('user', $user);
                        $query->setParameter('groupUser', $user->getGroupUsers());
                }

                $query->andWhere(':productFamily MEMBER OF e.productFamilies OR e.productFamilies is empty');
                $query->andWhere(':productCategory MEMBER OF e.productCategories OR e.productCategories is empty');
                $query->andWhere(':supplier MEMBER OF e.suppliers OR e.suppliers is empty');
                $query->setParameter('productFamily', $productFamilyIds['ids']);
                $query->setParameter('productCategory', $productFamilyIds['categories']);
                $query->setParameter('supplier', $productFamilyIds['suppliers']);
                $query->setParameter(':now', new \DateTime());

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


        }
}