<?php

namespace App\Repository;

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

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

    public function addPosition($annonce, $date_debut, $date_fin, $id_ligne, $user)
    {
        $position = new Position();
        $position->setAnnonce($annonce);
        $position->setDateDebut(new \DateTime($date_debut));
        $position->setDateFin(new \DateTime($date_fin));
        $position->setLigne($id_ligne);
        $position->setCreateBy($user);

        $this->_em->persist($position);
        $this->_em->flush();

        return $position;
    }


    public function getPositionByDate($date)
    {
        return $this->createQueryBuilder('p')
                    ->where('p.date_debut <= :DateSelect')
                    ->andWhere('p.date_fin >= :DateSelect')
                    ->setParameter('DateSelect', $date)
                    ->orderBy('p.ligne', 'ASC')
                    ->getQuery()
                    ->getResult()
            ;
    }

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

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