<?php

namespace Lc\CaracoleBundle\Repository\User;

use App\Entity\Merchant\Merchant;
use Lc\CaracoleBundle\Repository\MerchantRepositoryQueryTrait;
use Lc\SovBundle\Repository\User\UserRepositoryQuery as SovUserRepositoryQuery;

class UserRepositoryQuery extends SovUserRepositoryQuery
{
    use MerchantRepositoryQueryTrait;

    protected $isJoinUserMerchants = false;
    protected $isJoinAddresses = false;


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

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

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

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

    public function filterMerchantIsActive(): self
    {
        $this->joinUserMerchants();
        return $this
            ->andWhere('userMerchants.active = 1');
    }

    public function filterByMerchant(Merchant $merchant): self
    {
        $this->joinUserMerchants();
        return $this
            ->andWhere('userMerchants.merchant = :merchant')
            ->setParameter('merchant', $merchant);
    }

    public function filterByExtraInfoZip(string $zip)
    {
        $this->joinAddresses();

        return $this
            ->andWhere('.extraInfoZip LIKE :extraInfoZip OR addresses.zip LIKE :extraInfoZip')
            ->setParameter('extraInfoZip', $zip);
    }

    public function filterByExtraInfoCity(string $city)
    {
        $this->joinAddresses();

        return $this
            ->andWhere('.extraInfoCity LIKE :extraInfoCity OR addresses.city LIKE :extraInfoCity')
            ->setParameter('extraInfoCity', $city);
    }
}