<?php
namespace App\Controller\Admin\Filter;

use App\Entity\Menu;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use App\Controller\Admin\FilterType\IdExtraFilterType;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDataDto;
use EasyCorp\Bundle\EasyAdminBundle\Filter\FilterTrait;
use App\Controller\Admin\FilterType\IdExtraTypeFilterType;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Filter\FilterInterface;

class IdExtraTypeFilter implements FilterInterface
{
    use FilterTrait;

    public static function new(string $propertyName, $label = null): self
    {
        return (new self())
            ->setFilterFqcn(__CLASS__)
            ->setProperty($propertyName)
            ->setLabel($label)
            ->setFormType(IdExtraTypeFilterType::class);
    }

    public function apply(QueryBuilder $queryBuilder, FilterDataDto $filterDataDto, ?FieldDto $fieldDto, EntityDto $entityDto): void
    {
        $queryBuilder
          ->leftJoin(
              Menu::class,
              'm',
              \Doctrine\ORM\Query\Expr\Join::WITH,
              'm.id = entity.menu'
          )
          ->andWhere(
            sprintf(
              'm.id = %s',
              $filterDataDto->getValue()
            )
        );
    }
}