<?php

declare(strict_types=1);

namespace App\Repository;

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

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

    public function countAdminsEcole(): int
    {
        return (int) $this->createQueryBuilder('user')
            ->select('COUNT(user.id)')
            ->andWhere('user.roles LIKE :role')
            ->setParameter('role', '%ROLE_ECOLE_ADMIN%')
            ->getQuery()
            ->getSingleScalarResult();
    }

    public function countAdminsForEcole(int $ecoleId): int
    {
        return (int) $this->createQueryBuilder('user')
            ->select('COUNT(user.id)')
            ->andWhere('user.ecole = :ecoleId')
            ->andWhere('user.roles LIKE :role')
            ->setParameter('ecoleId', $ecoleId)
            ->setParameter('role', '%ROLE_ECOLE_ADMIN%')
            ->getQuery()
            ->getSingleScalarResult();
    }
}
