<?php

namespace App\Repository;

use Symfony\Component\Yaml\Yaml;
use App\Entity\RestaurantCategory;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;

/**
 * @extends ServiceEntityRepository<RestaurantCategory>
 *
 * @method RestaurantCategory|null find($id, $lockMode = null, $lockVersion = null)
 * @method RestaurantCategory|null findOneBy(array $criteria, array $orderBy = null)
 * @method RestaurantCategory[]    findAll()
 * @method RestaurantCategory[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class RestaurantCategoryRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, RestaurantCategory::class);
    }

    public function add(RestaurantCategory $entity, bool $flush = false): void
    {
        $this->getEntityManager()->persist($entity);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    public function remove(RestaurantCategory $entity, bool $flush = false): void
    {
        $this->getEntityManager()->remove($entity);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    /**
     * @return RestaurantType[] Returns an array of Restaurant objects
     */
    public function ListAllActive(): array
    {
        $pathConf = Yaml::parse(file_get_contents(realpath(__DIR__.'/../..') . '/config/services.yaml'));
        $url = $_ENV['BASE_URL_UPLOAD'] . $pathConf['parameters']['app.category_uploads_path'];

        $query = $this->createQueryBuilder('t')
            ->select('t.id, t.name, t.isPrincipal,
            CONCAT(\''.$url.'\',t.image) as image')
            ->andWhere('t.isActive = 1')
            ->orderBy('t.name', 'ASC')
        ;

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

//    /**
//     * @return RestaurantCategory[] Returns an array of RestaurantCategory objects
//     */
//    public function findByExampleField($value): array
//    {
//        return $this->createQueryBuilder('r')
//            ->andWhere('r.exampleField = :val')
//            ->setParameter('val', $value)
//            ->orderBy('r.id', 'ASC')
//            ->setMaxResults(10)
//            ->getQuery()
//            ->getResult()
//        ;
//    }

//    public function findOneBySomeField($value): ?RestaurantCategory
//    {
//        return $this->createQueryBuilder('r')
//            ->andWhere('r.exampleField = :val')
//            ->setParameter('val', $value)
//            ->getQuery()
//            ->getOneOrNullResult()
//        ;
//    }
}
