<?php

namespace App\Patient\Repository;

use App\Patient\Entity\SessionAuditLog;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

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

    /** تاریخچه‌ی یک مراجعه (جدید → قدیم)، آرایه‌ای. */
    public function findBySessionUuid(string $sessionUuid): array
    {
        return $this->createQueryBuilder('l')
            ->select('l.field AS field', 'l.operation AS operation', 'l.oldValue AS old_value', 'l.newValue AS new_value', 'l.actorName AS actor_name', 'l.note AS note', 'l.createdAt AS created_at')
            ->join('l.session', 's')
            ->where('s.uuid = :uuid')->setParameter('uuid', $sessionUuid)
            ->orderBy('l.createdAt', 'DESC')->addOrderBy('l.id', 'DESC')
            ->getQuery()->getArrayResult();
    }
}
