<?php

namespace App\Repository;

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

/**
 * @extends ServiceEntityRepository<Supervision>
 */
class SupervisionRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Supervision::class);
    }

    public function getSupervisorByWorker(User $worker): ?User
    {
        $qb = $this->createQueryBuilder('s')
            ->andWhere('s.worker = :worker')
            ->setParameter('worker', $worker->getId()->toBinary())
            ->getQuery()
            ->getResult()
        ;

        if (count($qb) > 0) {
            return $qb[0]->getSupervisor();
        }
        return null;
    }

    //    /**
    //     * @return Supervision[] Returns an array of Supervision 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): ?Supervision
    //    {
    //        return $this->createQueryBuilder('s')
    //            ->andWhere('s.exampleField = :val')
    //            ->setParameter('val', $value)
    //            ->getQuery()
    //            ->getOneOrNullResult()
    //        ;
    //    }
}
