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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\DataTransformer\JsonModelTransformerFactory;
use CpCreation\VitiCore\Cart\Model\Payment\Etransactions;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class EtransactionsPaymentType extends AbstractType implements PaymentMethodInterface
{
    public const TYPE = 'e-transactions';

    public function __construct(
        private readonly JsonModelTransformerFactory $transformers,
    ) {
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'Nom',
                'required' => true,
            ])
            ->add('subtitle', TextType::class, [
                'label' => 'Sous-titre affiché au checkout',
                'required' => false,
            ])
            ->add('production', ChoiceType::class, [
                'choices' => [
                    'production' => true,
                    'test' => false,
                ],
                'label' => 'Production',
                'required' => true,
            ])
            ->add('site', TextType::class, [
                'label' => 'Site',
                'required' => true,
            ])
            ->add('rank', TextType::class, [
                'label' => 'Rang',
                'required' => true,
            ])
            ->add('login', TextType::class, [
                'label' => 'Identifiant',
                'required' => true,
            ])
            ->add('hmacKey', TextType::class, [
                'label' => 'HMAC KEY',
                'required' => true,
            ])
            ->add('hmacAlgo', ChoiceType::class, [
                'choices' => [
                    'SHA512' => 'SHA512',
                ],
                'label' => 'HMAC ALGO',
                'required' => true,
            ])
            ->add('publicKey', TextareaType::class, [
                'label' => 'Clé publique Up2pay e-Transactions',
                'help' => 'Clé publique PEM utilisée pour vérifier la signature des retours banque.',
                'required' => false,
            ]);

        EstimatedTransactionFeeFields::add($builder);

        $builder->addModelTransformer(
            $this->transformers->for(Etransactions::class)
        );
    }

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

    public function getMethod(): string
    {
        return self::TYPE;
    }

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

    public function getFormType(): string
    {
        return self::class;
    }
}
