<?php

declare(strict_types=1);

namespace App\Repository;

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

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

    /** @return list<Niveau> */
    public function findForEcole(Ecole $ecole, bool $activeOnly = false): array
    {
        $qb = $this->createQueryBuilder('niveau')
            ->leftJoin('niveau.section', 'section')->addSelect('section')
            ->andWhere('niveau.ecole = :ecole')
            ->setParameter('ecole', $ecole)
            ->orderBy('niveau.ordre', 'ASC')
            ->addOrderBy('niveau.nom', 'ASC');
        if ($activeOnly) { $qb->andWhere('niveau.active = :active')->setParameter('active', true); }
        return $qb->getQuery()->getResult();
    }

    public function countForEcole(Ecole $ecole): int
    {
        return (int) $this->createQueryBuilder('niveau')->select('COUNT(niveau.id)')
            ->andWhere('niveau.ecole = :ecole')->setParameter('ecole', $ecole)
            ->getQuery()->getSingleScalarResult();
    }
}
