<?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 CpCreation\VitiCore\Product\Model\ProductPrice;
use CpCreation\VitiCore\Product\Model\ProductPriceGroup;
use CpCreation\VitiCore\Product\Repository\ProductPriceGroupRepository;
use CpCreation\VitiCore\Versioning\EcommerceVersionRecorder;
use CpCreation\VitiCore\Versioning\Repository\BusinessVersionRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class ProductPriceType extends AbstractType
{
    public function __construct(
        private readonly BusinessVersionRepository $businessVersionRepository,
    ) {
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add(
                'group', EntityType::class, [
                    'class' => ProductPriceGroup::class,
                    'query_builder' => function (ProductPriceGroupRepository $er) {
                        return $er->findByNameOrderAdminQuery();
                    },
                    'disabled' => true,
                    'attr' => [
                        'class' => 'hidden',
                    ],
                    'label' => false,
                    'choice_label' => 'name',
                ]
            )
            ->add(
                'price', MoneyType::class, [
                    'required' => true,
                    'label' => 'Prix TTC ou HT (Pro)',
                    'divisor' => 100,
                ]
            )
            ->add(
                'priceDiscount', MoneyType::class, [
                    'required' => false,
                    'label' => 'Prix Remisé TTC ou HT (Pro)',
                    'divisor' => 100,
                ]
            )
            ->add(
                'scheduledPrice', MoneyType::class, [
                    'required' => false,
                    'mapped' => false,
                    'label' => 'Prix futur TTC ou HT (Pro)',
                    'divisor' => 100,
                    'help' => 'Renseigner ce champ avec une date pour planifier un changement sans modifier le prix actuel.',
                ]
            )
            ->add(
                'scheduledPriceDiscount', MoneyType::class, [
                    'required' => false,
                    'mapped' => false,
                    'label' => 'Prix remisé futur TTC ou HT (Pro)',
                    'divisor' => 100,
                ]
            )
            ->add(
                'scheduledValidFrom', DateTimeType::class, [
                    'required' => false,
                    'mapped' => false,
                    'label' => 'Publier ce prix le',
                    'input' => 'datetime_immutable',
                    'widget' => 'single_text',
                    'html5' => true,
                ]
            );
    }

    public function buildView(FormView $view, FormInterface $form, array $options): void
    {
        $price = $form->getData();
        $view->vars['priceGroupName'] = $price instanceof ProductPrice ? $price->getGroup()?->getName() : null;
        $view->vars['scheduledBusinessVersion'] = $price instanceof ProductPrice
            ? $this->businessVersionRepository->findPendingDraft(
                ProductPrice::class,
                $price->getId()->toString(),
                EcommerceVersionRecorder::SCOPE_PRODUCT_PRICE,
            )
            : null;
    }

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