<?php

namespace App\Repository\Properties;

use Doctrine\ORM\NoResultException;
use App\Entity\Properties\Properties;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\NonUniqueResultException;
use Symfony\Component\Security\Core\Security;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;

/**
 * @method Properties|null find($id, $lockMode = null, $lockVersion = null)
 * @method Properties|null findOneBy(array $criteria, array $orderBy = null)
 * @method Properties[]    findAll()
 * @method Properties[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class PropertiesRepository extends ServiceEntityRepository
{
    private Security $security;
    public function __construct(ManagerRegistry $registry, Security $security)
    {
        parent::__construct($registry, Properties::class);
        $this->security = $security;
    }

    /**
     * @return Properties
     */
    public function new(): Properties
    {
        $propertie = new Properties();
        $propertie->setIsActif(true);
        return $propertie;
    }

    /**
     * @return Properties[]
     */
    public function getAll(): array
    {
        return $this->createQueryBuilder('p')
            ->andWhere('p.is_actif = TRUE')
            ->andWhere('p.deletedAt IS NULL')
            ->orderBy('p.updatedAt', 'DESC')
            ->getQuery()
            ->getResult()
        ;
    }

    /**
     * @return Properties[]|null
     */
    public function latest(int $limit = 5): ?array
    {
        $user = $this->security->getUser();

        // return $this->createQueryBuilder('p')
        //     ->andWhere('p.deletedAt IS NULL')
        //     ->setMaxResults($limit)
        //     ->orderBy('p.id', 'DESC')
        //     ->getQuery()
        //     ->getResult()
        // ;

        $db = $this->createQueryBuilder('p')
            ->andWhere('p.deletedAt IS NULL')
            ->setMaxResults($limit)
            ->orderBy('p.id', 'DESC');

        //Application du filtre par companyId selon le rôle
        if (!in_array('ROLE_SUPER_ADMIN', $user->getRoles())) {
            $db->andWhere('p.companyId = :companyId')
                ->setParameter('companyId', $user->getCompanyId());
        }

        return $db->getQuery()->getResult();
    }

    /**
     * @return int
     * @throws NoResultException
     * @throws NonUniqueResultException
     */
    public function getPropertiesUsed(?int $companyId = null): int
    {
        // return $this->createQueryBuilder('p')
        //     ->select('COUNT(p.id)')
        //     ->andWhere('p.locataires IS NOT NULL')
        //     ->andWhere('p.is_actif = TRUE')
        //     ->andWhere('p.deletedAt IS NULL')
        //     ->getQuery()
        //     ->getSingleScalarResult()
        //     ;

        $qb = $this->createQueryBuilder('p')
            ->select('COUNT(p.id)')
            ->andWhere('p.locataires IS NOT NULL')
            ->andWhere('p.is_actif = TRUE')
            ->andWhere('p.deletedAt IS NULL');

        if ($companyId !== null) {
            $qb->andWhere('p.companyId = :companyId')
                ->setParameter('companyId', $companyId);
        }

        return $qb->getQuery()->getSingleScalarResult();
    }

    public function getPropertiesByCompany(int $companyId)
    {
        return $this->createQueryBuilder('p')
            ->join('p.proprietaires', 'pr')
            ->where('pr.companyId = :companyId')
            ->setParameter('companyId', $companyId)
            ->getQuery()
            ->getResult();
    }
}
