<?php

namespace App\Rating\Repository;

use App\Doctor\Entity\Doctor;
use App\Rating\Entity\Comment;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

    public function findByUuid(string $uuid): ?Comment { return $this->findOneBy(['uuid' => $uuid]); }

    /** @return Comment[] */
    public function findApprovedByDoctor(Doctor $doctor): array
    {
        return $this->findBy(['doctor' => $doctor, 'status' => Comment::STATUS_APPROVED], ['createdAt' => 'DESC']);
    }

    /** @return Comment[] approved root comments (no parent) for a doctor */
    public function findApprovedRootsByDoctor(Doctor $doctor): array
    {
        return $this->findBy(
            ['doctor' => $doctor, 'status' => Comment::STATUS_APPROVED, 'parent' => null],
            ['createdAt' => 'DESC']
        );
    }

    /** @return Comment[] */
    public function findPending(): array
    {
        return $this->findBy(['status' => Comment::STATUS_PENDING], ['createdAt' => 'ASC']);
    }

    public function save(Comment $e, bool $flush = true): void { $this->getEntityManager()->persist($e); if ($flush) $this->getEntityManager()->flush(); }
    public function remove(Comment $e, bool $flush = true): void { $this->getEntityManager()->remove($e); if ($flush) $this->getEntityManager()->flush(); }
}
