<?php

namespace Lc\ShopBundle\Repository;

use Doctrine\ORM\QueryBuilder;
use Lc\ShopBundle\Context\DefaultRepositoryInterface;
use Lc\ShopBundle\Context\ProductCategoryInterface;

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

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

        public static function adminQueryBuilderForTree(BaseRepository $e): QueryBuilder
        {
                return $e->createQueryBuilder('e')
                        ->where('e.parent is NULL')
                        ->andWhere('e.status >= 0')
                        ->orderBy('e.position', 'ASC');
        }

        public function findAllParents($withOffline = false)
        {
                $query = $this->findByMerchantQuery()
                        ->andWhere('e.parent is NULL');

                if ($withOffline) $query->andWhere('e.status >= 0');
                else $query->andWhere('e.status = 1');

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

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

        }

        public function findAllByParent($parentCategory, $withOffline = false)
        {
                $query = $this->createQueryBuilder('e');
                $query->andWhere('e.parent = :idParentCategory');

                if ($withOffline) $query->andWhere('e.status >= 0');
                else $query->andWhere('e.status = 1');

                $query->setParameter('idParentCategory', $parentCategory->getId());
                return $query->getQuery()->getResult();
        }
}
