<?php

namespace App\Repository;


use Doctrine\Common\Collections\ArrayCollection;
use Lc\SovBundle\Repository\AbstractStore;
use Lc\SovBundle\Repository\RepositoryQueryInterface;
use Lc\SovBundle\Repository\StoreInterface;

class SearchStore extends AbstractStore implements StoreInterface
{

    public function __construct($query)
    {
        $this->query = $query;
    }

    public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
    {
        $query->orderBy('id');
        return $query;
    }

    public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
    {
        $query->filterIsValid();
        return $query;
    }

    public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
    {
        return $query;
    }

    public function filterSearch(
        ?string $description,
        ArrayCollection $territoryArray,
        ArrayCollection $thematicArray,
        $query = null
    ) {
        $query = $this->createDefaultQuery($query);

        if (!($thematicArray->isEmpty())) {
            $query->filterByThematic($thematicArray);
        }
        if (!($territoryArray->isEmpty())) {
            $query->filterByTerritory($territoryArray);
        }
        if (!empty($description)) {
            $query->filterByDescription($description);
        }

        return $query->find();
    }

}