<?php

namespace Lc\ShopBundle\Repository;

use Lc\ShopBundle\Context\DefaultRepositoryInterface;
use Lc\ShopBundle\Context\ProductFamilyInterface;
use Lc\ShopBundle\Context\ReductionCatalogInterface;
use Lc\ShopBundle\Model\ProductFamily;

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

        public function joinRelations($query)
        {
                $query->leftJoin('e.productCategories', 'cat');
                $query->addSelect('cat') ;

                $query->innerJoin('e.products', 'pfp');
                $query->addSelect('pfp') ;

                $query->orderBy('e.position', 'ASC');

                return $query ;
        }

        public function findAll()
        {
                $query = $this->findByMerchantQuery() ;
                $query = $this->joinRelations($query) ;
                return $query->getQuery()->getResult() ;
        }

        public function findAllOnline()
        {
                $query = $this->findByMerchantQuery() ;
                $query = $this->joinRelations($query) ;
                $query->andWhere('e.status = 1') ;
                $query->orderBy('RAND()');
                return $query->getQuery()->getResult() ;
        }

        public function findOneBySlug($slug)
        {
                $query = $this->findByMerchantQuery() ;
                $query->andWhere('e.slug = :slug')->setParameter('slug',$slug) ;
                $query->andWhere('e.status = 1');
                return $query->getQuery()->getOneOrNullResult() ;
        }

        public function getProductFamiliesByCategory($category){
                $query = $this->findByMerchantQuery() ;
                $query = $this->joinRelations($query) ;

                $query->andWhere(':category MEMBER OF e.productCategories');
                $query->setParameter('category', $category->getId());

                $query->andWhere('e.status = 1');
                $query->orderBy('e.position', 'ASC');

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

        public function getProductFamiliesNovelties(){
                $query = $this->findByMerchantQuery();
                $query = $this->joinRelations($query) ;

                $query->andWhere('e.status = 1');
                $query->andWhere(':now <= e.propertyNoveltyExpirationDate')
                        ->setParameter('now', new \DateTime()) ;
                $query->orderBy('RAND()');

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

        public function getProductFamiliesLargeVolumes(){
                $query = $this->findByMerchantQuery() ;
                $query = $this->joinRelations($query) ;

                $query->andWhere('e.status = 1');
                $query->andWhere('e.propertyLargeVolume = 1');

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

        public function getProductFamiliesOrganics(){
                $query = $this->findByMerchantQuery() ;
                $query = $this->joinRelations($query) ;

                $query->andWhere('e.status = 1');
                $query->andWhere('e.propertyOrganicLabel IS NOT NULL');

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

        public function findByTerms($terms, $maxResults = false)
        {
                $query = $this->findByMerchantQuery() ;

                $query->leftJoin('e.productCategories', 'cat');
                $query->addSelect('cat') ;

                $query->orderBy('e.position', 'ASC');

                $query->andWhere('e.status = 1');
                $query->andWhere('e.title LIKE :terms OR cat.title LIKE :terms');
                $query->setParameter(':terms', '%'.$terms.'%') ;

                $query->groupBy('e.id') ;

                if($maxResults) {
                        $query->setMaxResults($maxResults) ;
                }

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

        public function getProductFamiliesBySection($section, $maxResults = false, $sortField = 'productCategories.position', $sortDirection = 'ASC')
        {
                $query = $this->findByMerchantQuery() ;
                $query = $this->joinRelations($query) ;

                $query->andWhere('e.status = 1');
                $query->andWhere(':section MEMBER OF e.sections')
                        ->setParameter('section', $section) ;
                $query->leftJoin('e.productCategories', 'productCategories');
                if($sortField){
                        $query->orderBy($sortField,$sortDirection);
                }
                if($maxResults) {
                        $query->setMaxResults($maxResults) ;
                }

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


}
