<?php

namespace App\Form;

use App\Entity\Client;
use App\Entity\File;
use App\Entity\Role;
use App\Entity\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Count;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstName', TextType::class, [
                'label' => 'First Name',
            ])
            ->add('lastName', TextType::class, [
                'label' => 'Last Name',
            ])
            ->add('email', EmailType::class, [
                'label' => 'Email',
                'help' => 'Enter an email address',
            ])
            ->add('jobTitle', ChoiceType::class, [
                'label' => 'Job Title',
                'choices' => [
                    'Content Writer' => 'Content Writer',
                    'Technical' => 'Technical',
                    'PPC' => 'PPC',
                    'Account Manager' => 'Account Manager',
                    'Director' => 'Director',
                    'Client' => 'Client',
                ],
            ])
            ->add('groups', EntityType::class, [
                'label' => 'Roles',
                'class' => Role::class,
                'choice_value' => 'id',
                'choice_label' => 'title',
                'multiple' => true,
                'constraints' => [
                    new Count([
                        'min' => 1,
                        'minMessage' => 'At least 1 group is required',
                    ]),
                ],
            ])
            ->add('client', EntityType::class, [
                'label' => 'Linked Client account',
                'class' => Client::class,
                'choice_value' => 'id',
                'choice_label' => 'title',
                'required' => false,
            ])
            ->add('active', ChoiceType::class, [
                'label' => 'Is Active?',
                'choices' => ['No' => 0, 'Yes' => 1],
                'required' => false,
                'empty_data' => '1',
            ])
            ->add('avatar', EntityType::class, [
                'label' => 'Avatar',
                'class' => File::class,
                'choice_value' => 'id',
                'choice_label' => 'filename',
                'required' => false,
            ]);
    }

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