<?php

namespace App\Service\Doctrine;

use App\Filter\Doctrine\TimeConstraint\AbstractTimeFilter;
use App\Filter\Doctrine\TimeConstraint\ActivityYearFilter;
use App\Filter\Doctrine\TimeConstraint\DatetimeFilter;
use App\Service\Constraint\AbstractTimeConstraintUtils;
use App\Service\Constraint\ActivityYearConstraint;
use App\Service\Constraint\DateTimeConstraint;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Filter\SQLFilter;
use Doctrine\ORM\Query\FilterCollection;

/**
 * Service de configuration des filtres doctrine
 *
 * @see src/EventListener/OnKernelRequestPreRead.php
 */
class FiltersConfigurationService
{
    public function __construct(
        private EntityManagerInterface $entityManager,
        private DateTimeConstraint $dateTimeConstraint,
        private ActivityYearConstraint $activityYearConstraint
    ) {}

    /**
     * Return the existing Doctrine SQLFilters
     * @return FilterCollection
     */
    protected function getFilters(): FilterCollection {
        return $this->entityManager->getFilters();
    }

    /**
     * Enable and preconfigure the doctrine sql filters that apply the time constraints
     *
     * @param int $accessId
     * @return void
     */
    public function configureTimeConstraintFilters(int $accessId): void {
        /** @var DatetimeFilter $dateTimeFilter */
        $dateTimeFilter = $this->getFilters()->enable('date_time_filter');
        $dateTimeFilter->setAccessId($accessId);
        $dateTimeFilter->setTimeConstraint($this->dateTimeConstraint);

        /** @var ActivityYearFilter $activityYearFilter */
        $activityYearFilter = $this->getFilters()->enable('activity_year_filter');
        $activityYearFilter->setAccessId($accessId);
        $activityYearFilter->setTimeConstraint($this->activityYearConstraint);
    }
}
