<?php

namespace Lc\CaracoleBundle\Repository\User;

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

class UserMerchantRepositoryQuery extends AbstractRepositoryQuery
{
    use MerchantRepositoryQueryTrait;

    protected $isJoinUser = false;


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

    public function filterByUser(UserInterface $user)
    {
        return $this
            ->andWhere('.user = :user')
            ->setParameter('user', $user);
    }

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

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

    public function filterByFirstname(string $firstname)
    {
        $this->joinUser();

        return $this
            ->andWhere('user.firstname LIKE :firstname')
            ->setParameter('firstname', $firstname);
    }

    public function filterByLastname(string $lastname)
    {
        $this->joinUser();

        return $this
            ->andWhere('user.lastname LIKE :lastname')
            ->setParameter('lastname', $lastname);
    }

    public function filterByEmail(string $email)
    {
        $this->joinUser();

        return $this
            ->andWhere('user.email LIKE :email')
            ->setParameter('email', $email);
    }

    public function filterIsCreditActive()
    {
        return $this
                ->andWhere('.creditActive = 1');
    }
}