<?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\Consent;

use Doctrine\ORM\QueryBuilder;
use Ferienpass\AdminBundle\Form\Filter\AbstractFilterType;
use Ferienpass\AdminBundle\Form\Filter\AsSubFilter;
use Ferienpass\CoreBundle\Entity\AgreementLetterSignature;
use Ferienpass\CoreBundle\Entity\Consent;
use Ferienpass\CoreBundle\Entity\HostConsent;
use Ferienpass\CoreBundle\Entity\MedicalCareObjection;
use Ferienpass\CoreBundle\Entity\PhotoPermission;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

#[AsSubFilter(key: 'consentType', section: 'type', priority: 50, views: ['admin_consents_index'])]
class TypeFilter extends AbstractFilterType
{
    public function __construct(private readonly TranslatorInterface $translator)
    {
    }

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

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'choices' => [
                'edition_agreement_letter' => AgreementLetterSignature::class,
                'host_consent' => HostConsent::class,
                'photo_permission' => PhotoPermission::class,
                'medical_care_objection' => MedicalCareObjection::class,
                'form_consent' => Consent::class,
            ],
            'choice_label' => static fn (string $class, string $key): TranslatableMessage => new TranslatableMessage('consent.'.$key, [], 'admin'),
            'placeholder' => '-',
        ]);
    }

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

        $class = $form->getData();
        if (null === $class) {
            return;
        }

        if (Consent::class === $class) {
            $qb->andWhere($qb->expr()->andX(
                $qb->expr()->isInstanceOf('i', $class),
                $qb->expr()->not($qb->expr()->isInstanceOf('i', AgreementLetterSignature::class)),
                $qb->expr()->not($qb->expr()->isInstanceOf('i', HostConsent::class)),
                $qb->expr()->not($qb->expr()->isInstanceOf('i', PhotoPermission::class)),
                $qb->expr()->not($qb->expr()->isInstanceOf('i', MedicalCareObjection::class))
            ));
        } else {
            $qb->andWhere($qb->expr()->isInstanceOf('i', $class));
        }
    }

    protected function getHumanReadableValue(FormInterface $form): string|TranslatableInterface|null
    {
        $class = $form->getData();
        if (null === $class) {
            return null;
        }

        $choices = [
            AgreementLetterSignature::class => 'edition_agreement_letter',
            HostConsent::class => 'host_consent',
            PhotoPermission::class => 'photo_permission',
            MedicalCareObjection::class => 'medical_care_objection',
            Consent::class => 'form_consent',
        ];

        return $this->translator->trans('consent.'.$choices[$class], [], 'admin');
    }
}
