<?php

namespace App\Repository;

use App\Collection\TaskCollection;
use App\Entity\Task;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

    public function currentTasks(): TaskCollection
    {
        $from = new \DateTimeImmutable('today');
        $to = $from->modify('+14 days');

        $tasks = $this->createQueryBuilder('t')
            ->andWhere('t.date IS NULL OR (t.date >= :from AND t.date <= :to)')
            ->setParameter('from', $from)
            ->setParameter('to', $to)
            ->getQuery()
            ->getResult();

        return new TaskCollection($tasks);
    }

    public function allTasks(): TaskCollection
    {
        return new TaskCollection(parent::findAll());
    }
}
