<?php

namespace Lc\CaracoleBundle\Repository\Credit;

use App\Entity\Merchant\Merchant;
use App\Entity\User\UserMerchant;
use Knp\Component\Pager\PaginatorInterface;
use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
use Lc\SovBundle\Repository\AbstractRepositoryQuery;
use DateTime;

class CreditHistoryRepositoryQuery extends AbstractRepositoryQuery
{
    protected $isJoinUserMerchants = false;

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

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

            return $this
                ->innerJoin('.userMerchant', 'um');
        }
        return $this;
    }

    public function filterByMerchant(MerchantInterface $merchant): self
    {
        $this->joinUserMerchant();
        return $this
            ->andWhereMerchant('um', $merchant);
    }

    public function filterByUserMerchant(UserMerchantInterface $userMerchant): self
    {
        return $this
            ->andWhere('.userMerchant = :userMerchant')
            ->setParameter('userMerchant', $userMerchant);
    }

    public function filterByDateStart(DateTime $dateStart): self
    {
        return $this
            ->andWhere('.createdAt >= :dateStart')
            ->setParameter(':dateStart', $dateStart);
    }

    public function filterByDateEnd(DateTime $dateEnd): self
    {
        return $this
            ->andWhere('.createdAt <= :dateEnd')
            ->setParameter(':dateEnd', $dateEnd);
    }
}