<?php


namespace Shop\Infrastructure\Repositories\Provider;


use Diamond\Repositories\PropelAbstractRepository;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Repo\CollectionInterface;
use Repo\CrudRepositoryBuilderInterface;
use Repo\EntityInterface;
use Repo\PaginationInterface;
use Shop\Domain\Provider\Contracts\ProviderCriteriaInterface;
use Shop\Domain\Provider\ProviderCollection;
use Shop\Infrastructure\Models\Customer\Customer;
use Shop\Infrastructure\Models\Provider\Provider;
use Shop\Infrastructure\Models\Provider\ProviderQuery;

class ProviderRepository extends PropelAbstractRepository implements CrudRepositoryBuilderInterface
{
    public static function createEntity(): EntityInterface
    {
        return new Provider();
    }

    protected function createQuery(): ModelCriteria
    {
        return ProviderQuery::create();
    }

    /**
     * @param EntityInterface $entity
     * @throws \Propel\Runtime\Exception\PropelException
     */
    public function delete(EntityInterface $entity): void
    {
        /**
         * @var Customer $contractor
         */
        $contractor = $entity->getContractor();
        $contractor->setBlocked('Y');
        $contractor->save();

        parent::delete($entity); // TODO: Change the autogenerated stub
    }

    /**
     * @param PaginationInterface|ProviderCriteriaInterface $criteria
     * @param ModelCriteria|ProviderQuery $dbCriteria
     */
    protected function modifyCriteria(PaginationInterface $criteria, ModelCriteria $dbCriteria): void
    {
        
        $dbCriteria

            ->_if($criteria->getSortByName() !== null)
            ->orderByName($criteria->getSortByName())
            ->_endif()

            ->_if($criteria->getSearchByDestination()!==null)
            ->filterByName('%'. $criteria->getSearchByDestination() . '%', Criteria::LIKE)
            ->_endif()

            ->_if($criteria->getSearchByName()!==null)
            ->filterByName('%'. $criteria->getSearchByName() . '%', Criteria::LIKE)
            ->_endif()

            ->_if($criteria->getFilterByName()!==null)
            ->filterByName($criteria->getFilterByName())
            ->_endif()
        ;
    }


    protected function createCollection(): CollectionInterface
    {
        return new ProviderCollection();
    }

}