<?php

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\SystemPay;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class SystemPayPaymentType extends AbstractType implements PaymentMethodInterface
{
    public const TYPE = 'systempay';

    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('mode', ChoiceType::class, [
                'label' => 'Production',
                'choices' => [
                    'production' => 'PRODUCTION',
                    'test' => 'TEST',
                ],
                'required' => true,
            ])
            ->add('siteId', TextType::class, [
                'label' => 'Identifiant de la boutique',
                'required' => true,
            ])
            ->add('key', TextType::class, [
                'label' => 'Clé',
                'required' => true,
            ])
            ->add('hmacAlgo', ChoiceType::class, [
                'choices' => [
                    'SHA1' => 'SHA1',
                    'HMAC-SHA-256' => 'sha256',
                ],
                'label' => 'HMAC ALGO',
                'required' => true,
            ]);

        EstimatedTransactionFeeFields::add($builder);

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

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

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

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

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