<?php

declare(strict_types=1);

namespace App\Form;

use App\Entity\Theme;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;

final class ThemeType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('nom', TextType::class, [
                'label' => 'Nom du thème',
                'attr' => ['placeholder' => 'Thème Qamar'],
            ])
            ->add('code', TextType::class, [
                'label' => 'Code technique',
                'attr' => ['placeholder' => 'qamar'],
                'help' => 'Ce code charge le dossier Twig/CSS du thème public.',
            ])
            ->add('description', TextareaType::class, [
                'label' => 'Description',
                'required' => false,
                'attr' => ['rows' => 4, 'placeholder' => 'Décrivez la direction visuelle et le public adapté.'],
            ])
            ->add('previewImageFile', FileType::class, [
                'label' => 'Image d’aperçu',
                'required' => false,
                'mapped' => false,
                'help' => 'PNG, JPG, WEBP ou SVG. Le fichier actuel est conservé si aucun fichier n’est choisi.',
                'constraints' => [
                    new Assert\File(
                        maxSize: '3M',
                        mimeTypes: [
                            'image/png',
                            'image/jpeg',
                            'image/webp',
                            'image/svg+xml',
                        ],
                        mimeTypesMessage: 'Veuillez téléverser une image valide : PNG, JPG, WEBP ou SVG.',
                    ),
                ],
            ])
            ->add('actif', CheckboxType::class, [
                'label' => 'Thème actif',
                'required' => false,
            ]);
    }

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