<?php

namespace Lc\CaracoleBundle\Repository;

use Lc\CaracoleBundle\Model\Section\SectionInterface;
use Lc\SovBundle\Repository\RepositoryQueryInterface;
use Lc\SovBundle\Repository\StoreInterface;

trait SectionStoreTrait
{
    protected ?SectionInterface $section = null;

    public function setSection(?SectionInterface $section):self
    {
        $this->section = $section;

        return $this;
    }

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

    public function addFilterBySectionOptionnal(RepositoryQueryInterface $query): StoreInterface
    {
        if($this->isSectionDefined()) {
            $query->filterBySection($this->section);
        }

        return $this;
    }

    public function addFilterBySectionRequired(RepositoryQueryInterface $query): StoreInterface
    {
        $this->addFilterBySectionOptionnal($query);

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

        return $this;
    }

    public function getParents()
    {
        $query = $this->query->create();
        if ($this->section) {
            $query->filterBySection($this->section);
        }
        $query->filterIsParent();

        return $query->find();
    }

}