<?php

namespace AppBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * ChangeRequestRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ChangeRequestRepository extends EntityRepository
{
    /**
     * @return \Doctrine\ORM\Query
     */
    public function getUnresolvedChangeRequestsQuery()
    {
        $qb = $this->createQueryBuilder('c');

        return $qb
            ->join('c.adventure', 'a')
            ->where($qb->expr()->eq('c.resolved', $qb->expr()->literal(false)))
            ->getQuery();
    }

    public function resolveChangeRequestsByIds(array $ids, string $remarks = null): int
    {
        $qb = $this->createQueryBuilder('c');
        $qb
            ->update()
            ->set('c.resolved', $qb->expr()->literal(true))
            ->where($qb->expr()->in('c.id', ':ids'))
            ->setParameter('ids', $ids)
            ->andWhere($qb->expr()->eq('c.resolved', $qb->expr()->literal(false)));
        if (!empty($remarks)) {
            $qb
                ->set('c.curatorRemarks', ':remarks')
                ->setParameter('remarks', $remarks);
        }

        return $qb->getQuery()->execute();
    }
}
