<?php

declare(strict_types=1);

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

use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use CpCreation\VitiCore\Product\Model\ProductVariantOption;
use CpCreation\VitiCore\Product\Model\ProductVariantOptionImage;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class ProductVariantOptionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('translations', TranslationsType::class, [
                'label' => false,
                'fields' => [
                    'name' => [
                        'field_type' => TextType::class,
                        'label' => 'Nom',
                        'required' => true,
                    ],
                ],
            ])
            ->add('price', MoneyType::class, [
                'label' => 'Supplément TTC',
                'required' => false,
                'divisor' => 100,
            ])
            ->add('flatFee', CheckboxType::class, [
                'label' => 'Forfait',
                'required' => false,
                'help' => 'Coché: prix unique par ligne. Décoché: prix multiplié par la quantité.',
            ])
            ->add('soldOut', CheckboxType::class, [
                'label' => 'Épuisé',
                'required' => false,
            ])
            ->add('images', ImageCollectionType::class, [
                'label' => false,
                'entry_type' => ProductVariantOptionImageType::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => true,
                'by_reference' => false,
                'delete_empty' => true,
                'attr' => [
                    'data-name-prefix' => 'OptionImage',
                    'class' => 'image-collection',
                ],
                'entry_options' => [
                    'data_class' => ProductVariantOptionImage::class,
                    'label' => false,
                ],
            ])
            ->add('position', HiddenType::class, [
                'required' => false,
                'attr' => ['class' => 'position-form'],
            ]);
    }

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