<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\AdminBundle\Form\Filter\Offer;

use Doctrine\ORM\QueryBuilder;
use Ferienpass\AdminBundle\Form\Filter\AbstractFilterType;
use Ferienpass\AdminBundle\Form\Filter\AsSubFilter;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Contracts\Translation\TranslatableInterface;

#[AsSubFilter(key: 'attendancesWaiting', section: 'toggles', priority: 81, views: ['admin_offers_index'])]
class AttendancesWaitingFilter extends AbstractFilterType
{
    public function __construct(private readonly Security $security)
    {
    }

    public function shallDisplay(?string $viewId = null): bool
    {
        return $this->security->isGranted('ROLE_ADMIN');
    }

    public function getParent(): string
    {
        return CheckboxType::class;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'label' => 'Unbearbeitete Plätze',
        ]);
    }

    public function apply(QueryBuilder $qb, FormInterface $form): void
    {
        if (!$form->getData()) {
            return;
        }

        $qb
            ->innerJoin('i.attendances', 'filter_a')
            ->andWhere('filter_a.status = :waiting_status')
            ->setParameter('waiting_status', 'waiting')
        ;
    }

    protected function getHumanReadableValue(FormInterface $form): string|TranslatableInterface|null
    {
        return new TranslatableMessage('offers.filter.attendancesWaiting', domain: 'admin');
    }
}
