<?php

namespace Lc\CaracoleBundle\Repository;

use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
use Lc\SovBundle\Repository\RepositoryQueryInterface;
use Lc\SovBundle\Repository\StoreInterface;

trait MerchantStoreTrait
{
    protected ?MerchantInterface $merchant = null;

    public function setMerchant(?MerchantInterface $merchant):self
    {
        $this->merchant = $merchant;

        return $this;
    }

    public function isMerchantDefined(): bool
    {
        return isset($this->merchant) && $this->merchant;
    }

    public function addFilterByMerchantOptionnal(RepositoryQueryInterface $query): StoreInterface
    {
        if($this->isMerchantDefined()) {
            $query->filterByMerchant($this->merchant);
        }

        return $this;
    }

    public function addFilterByMerchantRequired(RepositoryQueryInterface $query): StoreInterface
    {
        $this->addFilterByMerchantOptionnal($query);

        if(!$this->isMerchantDefined()) {
            throw new \ErrorException('Le Merchant doit être définie dans '.get_class($this));
        }

        return $this;
    }

    public function addFilterByMerchantViaSectionOptionnal(RepositoryQueryInterface $query): StoreInterface
    {
        if($this->isMerchantDefined()) {
            $query->filterByMerchantViaSection($this->merchant);
        }

        return $this;
    }

    public function addFilterByMerchantViaSectionRequired(RepositoryQueryInterface $query): StoreInterface
    {
        $this->addFilterByMerchantOptionnal($query);

        if(!$this->isMerchantDefined()) {
            throw new \ErrorException('Le Merchant doit être définie dans '.get_class($this));
        }

        return $this;
    }
}