<?php

declare(strict_types=1);

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

use CpCreation\VitiCore\Marketing\Model\AbandonedCartRule;
use CpCreation\VitiCore\Product\Model\ProductPriceGroup;
use CpCreation\VitiCore\Product\Repository\ProductPriceGroupRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class AbandonedCartRuleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'marketing.abandoned_cart.rule.name',
            ])
            ->add('enabled', CheckboxType::class, [
                'label' => 'marketing.abandoned_cart.rule.enabled',
                'required' => false,
            ])
            ->add('position', IntegerType::class, [
                'label' => 'marketing.abandoned_cart.rule.position',
                'attr' => ['min' => 0],
            ])
            ->add('delayHours', IntegerType::class, [
                'label' => 'marketing.abandoned_cart.rule.delay_hours',
                'help' => 'marketing.abandoned_cart.rule.delay_hours_help',
                'attr' => ['min' => 1, 'max' => 2160],
            ])
            ->add('minSubtotalTTC', MoneyType::class, [
                'label' => 'marketing.abandoned_cart.rule.min_amount',
                'help' => 'marketing.abandoned_cart.rule.min_amount_help',
                'divisor' => 100,
                'currency' => 'EUR',
                'required' => false,
            ])
            ->add('maxSubtotalTTC', MoneyType::class, [
                'label' => 'marketing.abandoned_cart.rule.max_amount',
                'help' => 'marketing.abandoned_cart.rule.max_amount_help',
                'divisor' => 100,
                'currency' => 'EUR',
                'required' => false,
            ])
            ->add('countryCodes', CountryType::class, [
                'label' => 'marketing.abandoned_cart.rule.countries',
                'help' => 'marketing.abandoned_cart.rule.countries_help',
                'multiple' => true,
                'required' => false,
                'attr' => ['class' => 'select2'],
            ])
            ->add('customerType', ChoiceType::class, [
                'label' => 'marketing.abandoned_cart.rule.customer_type',
                'choices' => [
                    'marketing.abandoned_cart.customer.any' => AbandonedCartRule::CUSTOMER_ANY,
                    'marketing.abandoned_cart.customer.new' => AbandonedCartRule::CUSTOMER_NEW,
                    'marketing.abandoned_cart.customer.returning' => AbandonedCartRule::CUSTOMER_RETURNING,
                ],
            ])
            ->add('discountType', ChoiceType::class, [
                'label' => 'marketing.abandoned_cart.rule.discount_type',
                'choices' => [
                    'marketing.abandoned_cart.discount.none' => AbandonedCartRule::DISCOUNT_NONE,
                    'marketing.abandoned_cart.discount.fixed' => AbandonedCartRule::DISCOUNT_FIXED,
                    'marketing.abandoned_cart.discount.percent' => AbandonedCartRule::DISCOUNT_PERCENT,
                    'marketing.abandoned_cart.discount.free_shipping' => AbandonedCartRule::DISCOUNT_FREE_SHIPPING,
                ],
            ])
            ->add('priceGroup', EntityType::class, [
                'class' => ProductPriceGroup::class,
                'query_builder' => static function (ProductPriceGroupRepository $repository) {
                    return $repository->findByNameOrderAdminQuery();
                },
                'choice_label' => 'name',
                'label' => 'marketing.abandoned_cart.rule.price_group',
                'help' => 'marketing.abandoned_cart.rule.price_group_help',
                'required' => true,
                'attr' => ['class' => 'select2'],
            ])
            ->add('couponOnlyWithoutExistingDiscount', CheckboxType::class, [
                'label' => 'marketing.abandoned_cart.rule.coupon_only_without_existing_discount',
                'help' => 'marketing.abandoned_cart.rule.coupon_only_without_existing_discount_help',
                'required' => false,
            ])
            ->add('discountAmountTTC', MoneyType::class, [
                'label' => 'marketing.abandoned_cart.rule.discount_amount',
                'help' => 'marketing.abandoned_cart.rule.discount_amount_help',
                'divisor' => 100,
                'currency' => 'EUR',
                'required' => false,
            ])
            ->add('discountPercent', NumberType::class, [
                'label' => 'marketing.abandoned_cart.rule.discount_percent',
                'help' => 'marketing.abandoned_cart.rule.discount_percent_help',
                'scale' => 2,
                'html5' => true,
                'required' => false,
                'attr' => ['min' => 0.01, 'max' => 100, 'step' => 0.01],
            ])
            ->add('validityDays', IntegerType::class, [
                'label' => 'marketing.abandoned_cart.rule.validity_days',
                'help' => 'marketing.abandoned_cart.rule.validity_days_help',
                'attr' => ['min' => 1, 'max' => 90],
            ]);
    }

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