<?php

declare(strict_types=1);

namespace App\Repository;

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

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

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

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