<?php

namespace App\Repository;

use App\Entity\Menu;
use App\Entity\Size;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;

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

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

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

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

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

    /**
     * @return Size[] Returns an array of Size from id menu
     */
    public function getSize($id): array
    {
        $query = $this->createQueryBuilder('s')
            ->select('s.id, s.name, s.pvp')
            ->leftJoin(
                Menu::class,
                'm',
                \Doctrine\ORM\Query\Expr\Join::WITH,
                'm.id = s.menu'
            )
            ->andWhere('m.isActive = 1')
            ->andWhere('m.id = :id')
            ->setParameter('id', $id)

            ->orderBy('s.pvp, s.name', 'ASC')
        ;

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

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

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