<?php

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

use CpCreation\VitiCore\Product\Model\ProductPriceGroup;
use CpCreation\VitiCore\Product\Repository\ProductPriceGroupRepository;
use CpCreation\VitiCore\User\Model\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class ClientPartnerGroupType extends AbstractType
{
    public function __construct(
        private readonly ProductPriceGroupRepository $productPriceGroupRepository,
    ) {
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('group', EntityType::class, [
                'class' => ProductPriceGroup::class,
                'choices' => $this->productPriceGroupRepository->findByPositionOrderAdmin(),
                'choice_label' => 'name',
                'required' => false,
                'placeholder' => 'Aucun groupe partenaire',
                'label' => 'Groupe partenaire',
                'attr' => [
                    'class' => 'select2',
                    'data-allow-clear' => 'true',
                    'data-placeholder' => 'Aucun groupe partenaire',
                ],
            ])
            ->add('save', SubmitType::class, [
                'label' => 'Enregistrer',
                'attr' => ['class' => 'btn btn-primary'],
            ]);
    }

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