<?php

namespace App\Repository;

use App\Entity\Project;
use App\Entity\Task;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * @extends ServiceEntityRepository<Task>
 */
class TaskRepository extends ServiceEntityRepository
{
    protected PaginatorInterface $paginator;
    protected Request $request;

    public function __construct(ManagerRegistry $registry, PaginatorInterface $paginator, RequestStack $requestStack)
    {
        parent::__construct($registry, Task::class);

        $this->paginator = $paginator;
        $this->request = $requestStack->getCurrentRequest();
    }

    //    /**
    //     * @return Task[] Returns an array of Task objects
    //     */
    //    public function findByExampleField($value): array
    //    {
    //        return $this->createQueryBuilder('t')
    //            ->andWhere('t.exampleField = :val')
    //            ->setParameter('val', $value)
    //            ->orderBy('t.id', 'ASC')
    //            ->setMaxResults(10)
    //            ->getQuery()
    //            ->getResult()
    //        ;
    //    }

    //    public function findOneBySomeField($value): ?Task
    //    {
    //        return $this->createQueryBuilder('t')
    //            ->andWhere('t.exampleField = :val')
    //            ->setParameter('val', $value)
    //            ->getQuery()
    //            ->getOneOrNullResult()
    //        ;
    //    }

    public function findByProjectPaginated(Project $project)
    {
        $query = $this->createQueryBuilder('t')
                      ->andWhere('t.project = :project')
                      ->setParameter('project', $project)
                      ->getQuery();

        return $this->paginator->paginate(
            $query,
            $this->request->query->getInt('page', 1),
            100
        );
    }

    public function findNextOne(Task $task, $order = 'ASC')
    {
        return $this->createQueryBuilder('t')
                    ->andWhere('t.project = :project')
                    ->andWhere('t.id > :id')
                    ->setParameter('project', $task->getProject())
                    ->setParameter('id', $task->getId())
                    ->orderBy('t.id', $order)
                    ->setMaxResults(1)
                    ->getQuery()
                    ->getOneOrNullResult()
        ;
    }

    public function findRandomByProject(Project $project, $status = null)
    {
        $qb = $this->createQueryBuilder('t')
                   ->andWhere('t.project = :project')
                   ->setParameter('project', $project);

        if (!is_null($status)) {
            $qb->andWhere('t.status = :status')
               ->setParameter('status', $status);
        }

        $tasks = $qb->getQuery()
                    ->getResult()
        ;

        shuffle($tasks);

        return reset($tasks);
    }
}
