<?php

namespace Lc\SovBundle\Repository\Reminder;

use Knp\Component\Pager\PaginatorInterface;
use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Repository\AbstractRepositoryQuery;

class ReminderRepositoryQuery extends AbstractRepositoryQuery implements ReminderRepositoryQueryInterface
{
    protected $isJoinUser = false;

    public function __construct(ReminderRepository $repository, PaginatorInterface $paginator)
    {
        parent::__construct($repository, 'reminder', $paginator);
    }

    public function filterByDone($done = false): self
    {
        return $this
            ->andWhere('.done = :done')
            ->setParameter(':done', $done);
    }

    public function joinUser(): self
    {
        if (!$this->isJoinUser) {
            $this->isJoinUser = true;

            return $this
                ->leftJoin('.users', 'users');
        }
        return $this;
    }

    public function filterByUser(UserInterface $user): self
    {
        $this->joinUser();
        return $this
            ->having('COUNT(users.id) = 0')
            ->orHaving(':user MEMBER OF .users')
            ->setParameter(':user', $user);
    }

    public function filterByCrudAction(?string $crudAction= null): self
    {
        if (is_null($crudAction)) {
            return $this
                ->andWhere('.crudControllerFqcn IS NULL');
        } else {
            return $this
                ->andWhere('.crudAction = :crudAction')
                ->setParameter(':crudAction', $crudAction);
        }
    }

    public function filterByCrudControllerFqcn(?string $crudControllerFqcn= null): self
    {
        if (is_null($crudControllerFqcn)) {
            return $this
                ->andWhere('.crudControllerFqcn IS NULL');
        } else {
            return $this
                ->andWhere('.crudControllerFqcn = :crudControllerFqcn')
                ->setParameter(':crudControllerFqcn', $crudControllerFqcn);
        }
    }

    public function filterByEntityId(?int $entityId=null): self
    {
        if (is_null($entityId)) {
            return $this
                ->andWhere('.entityId IS NULL');
        } else {
            return $this
                ->andWhere('.entityId = :entityId')
                ->setParameter(':entityId', $entityId);
        }
    }

    public function filterIsNotDone(): self
    {
        return $this
            ->andWhere('.done = 0');
    }

    public function filterLikeCrudAction(string $crudAction): self
    {
        return $this
            ->andWhere('.crudAction LIKE :crudAction')
            ->setParameter('crudAction', $crudAction);
    }

    public function filterLikeCrudControllerFqcn(string $crudControllerFqcn): self
    {
        return $this
            ->andWhere('.crudControllerFqcn LIKE :crudControllerFqcn')
            ->setParameter('crudControllerFqcn', $crudControllerFqcn);
    }

    public function filterLikeEntityId(int $entityId): self
    {
        return $this
            ->andWhere('.entityId LIKE :id')
            ->setParameter('entityId', $entityId);
    }
}
