<?php

declare(strict_types=1);

namespace App\Repository;

use App\Entity\MailMessage;
use App\Entity\Task;
use App\Entity\TaskMailLink;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class TaskMailLinkRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, TaskMailLink::class);
    }

    /**
     * @return list<TaskMailLink>
     */
    public function findByTask(Task $task): array
    {
        return $this->createQueryBuilder('l')
            ->andWhere('l.task = :task')
            ->setParameter('task', $task)
            ->orderBy('l.linkedAt', 'DESC')
            ->getQuery()
            ->getResult()
        ;
    }

    public function findByTaskAndMessage(Task $task, MailMessage $message): ?TaskMailLink
    {
        return $this->findOneBy(['task' => $task, 'mailMessage' => $message]);
    }

    /**
     * @return list<TaskMailLink>
     */
    public function findByMessage(MailMessage $message): array
    {
        return $this->findBy(['mailMessage' => $message]);
    }
}
