<?php

declare(strict_types=1);

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\StripeModel;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;

final class StripePaymentType extends AbstractType implements PaymentMethodInterface
{
    public const TYPE = 'stripe';

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

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $webhookPath = $this->router->generate('stripe_responseAt', [], UrlGeneratorInterface::ABSOLUTE_PATH);
        $webhookUrlExample = 'https://votre-domaine.tld'.$webhookPath;
        $events = implode(', ', [
            'checkout.session.completed',
            'checkout.session.async_payment_succeeded',
            'checkout.session.async_payment_failed',
            'checkout.session.expired',
            'charge.refunded',
            'refund.updated',
            'refund.failed',
        ]);
        $webhookHelp = sprintf(
            'Webhook Stripe a configurer : <code>%s</code><br>Evenements a declarer : <code>%s</code><br>Mode dev : installer la CLI avec <code>brew install stripe/stripe-cli/stripe</code>, puis lancer <code>stripe listen --forward-to https://localhost%s</code>.<br>Dependance PHP du projet : <code>docker compose exec php composer update stripe/stripe-php cpcreation/viti-core --no-interaction</code>.',
            $webhookUrlExample,
            $events,
            $webhookPath
        );

        $builder
            ->add('name', TextType::class, [
                'label' => 'Nom',
                'required' => true,
            ])
            ->add('subtitle', TextType::class, [
                'label' => 'Sous-titre affiche au checkout',
                'required' => false,
            ])
            ->add('secretKey', TextType::class, [
                'label' => 'Cle secrete Stripe',
                'required' => true,
                'help' => 'Utiliser une cle sk_test_... en mode test ou sk_live_... en production.',
            ])
            ->add('publishableKey', TextType::class, [
                'label' => 'Cle publique Stripe',
                'required' => false,
                'help' => 'Optionnelle ici car le checkout utilise Stripe Checkout heberge, mais utile pour un futur front Stripe.js.',
            ])
            ->add('webhookSecret', TextType::class, [
                'label' => 'Secret de signature webhook',
                'required' => true,
                'help' => $webhookHelp,
                'help_html' => true,
            ])
            ->add('refundCreditNoteDirectly', CheckboxType::class, [
                'label' => 'Rembourser les avoirs par carte directement',
                'required' => false,
                'help' => 'Active le remboursement Stripe automatique lors de la creation d\'un avoir.',
            ]);

        EstimatedTransactionFeeFields::add($builder);

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

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

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

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

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