<?php

namespace App\Monitor\Framework\Repository;

use App\Monitor\Framework\Entity\Monitor;
use App\Util\Paginator;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @extends ServiceEntityRepository<Monitor>
 */
class MonitorRepository extends ServiceEntityRepository
{
    private Paginator $paginator;

    public function __construct(ManagerRegistry $registry, Paginator $paginator)
    {
        parent::__construct($registry, Monitor::class);
        $this->paginator = $paginator;
    }

    public function getUserMonitorsPaginated(UserInterface $user, int $page, int $perPage, string $searchTerm): Paginator
    {
        $query = $this->createQueryBuilder('m')
            ->andWhere('m.status IN (:statuses)')
            ->andWhere('m.user = :user')
            ->orderBy('m.id', 'ASC')
            ->setParameter('statuses', ['Active', 'New', 'In Progress', 'Complete'])
            ->setParameter('user', $user)
            ->getQuery();

        return $this->paginator->paginate($query, $page, $perPage);
    }
}
