<?php

declare(strict_types=1);

namespace App\Repository;

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

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

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

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