<?php

declare(strict_types=1);

namespace App\Module\ProjectManagement\Infrastructure\Doctrine;

use App\Module\ProjectManagement\Domain\Entity\TaskStatus;
use App\Module\ProjectManagement\Domain\Repository\TaskStatusRepositoryInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<TaskStatus>
 */
class DoctrineTaskStatusRepository extends ServiceEntityRepository implements TaskStatusRepositoryInterface
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, TaskStatus::class);
    }

    public function findById(int $id): ?TaskStatus
    {
        return $this->find($id);
    }

    public function findFirstNonFinal(): ?TaskStatus
    {
        return $this->createQueryBuilder('s')
            ->where('s.isFinal = :final')
            ->setParameter('final', false)
            ->orderBy('s.position', 'ASC')
            ->setMaxResults(1)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
}
