<?php

/*
 * This file is part of the Serenity package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin;

use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Manager\PaymentMethodManager;
use CpCreation\VitiCore\Cart\Model\PaymentMethod;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class PaymentMethodStep2Type extends AbstractType
{
    public function __construct(
        private readonly PaymentMethodManager $manager,
    ) {
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add('paymentOnline', CheckboxType::class, [
            'label' => 'Paiement utilisé pour le paiement en ligne d\'une facture',
            'required' => false,
        ]);
        $builder->add('image', ImageType::class, [
            'label' => 'Icône affichée au checkout',
            'required' => false,
            'title' => false,
        ]);

        $builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'onPreSetData']);
    }

    public function onPreSetData(FormEvent $event): void
    {
        $data = $event->getData();

        if (!$data instanceof PaymentMethod) {
            return;
        }

        $method = $data->getMethod();
        if (null === $method || '' === $method) {
            return;
        }

        $form = $event->getForm();

        // On récupère un "form type" à utiliser pour configuration
        $type = $this->manager->createFormPaymentMethodManager($method);

        /**
         * IMPORTANT Symfony 7:
         * - $type devrait idéalement être une FQCN (ex: MyConfigType::class)
         * - ou un objet "ResolvedFormTypeInterface" (rare)
         *
         * Si ton manager retourne encore un objet avec getName(),
         * on garde une compat fallback ci-dessous.
         */
        $fieldType = \is_string($type)
            ? $type
            : (method_exists($type, 'getName') ? $type->getName() : null);

        if (null === $fieldType) {
            return;
        }

        $form->add('translations', TranslationsType::class, [
            'label' => false,
            'fields' => [
                'configuration' => [
                    'field_type' => $fieldType,
                    'label' => false,
                    'required' => false,
                ],
            ],
        ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => PaymentMethod::class,
            'translation_domain' => 'form',
        ]);
    }

    public function getBlockPrefix(): string
    {
        return 'paymentMethodStep2';
    }
}
