<?php

namespace App\Form\User\Settings;

use Symfony\Component\Form\AbstractType;
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\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;

class ProfileType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('pseudo', TextType::class, [
                'disabled' => true,
                'label' => 'Pseudo',
                /*'constraints' => [
                    new Assert\NotBlank(),
                    new Assert\Length(min: 3, max: 30),
                    new Assert\Regex(
                        pattern: '/^[\w\-\.]+$/u',
                        message: 'Le pseudo ne peut contenir que des lettres, chiffres, tirets et points.'
                    ),
                ],*/
            ])
            ->add('bio', TextareaType::class, [
                'label' => 'Bio',
                'required' => false,
                'empty_data' => '',
                'constraints' => [
                    new Assert\Length(max: 500),
                ],
                'attr' => ['maxlength' => 500, 'rows' => 4],
            ])
            // url
            ->add('uri_pp', UrlType::class, [
                'label'            => 'URL de la photo de profil',
                'mapped'           => false,
                'required'         => false,
                'default_protocol' => 'https',
                'empty_data'       => '',
                'constraints'      => [
                    new Assert\Url(
                        protocols: ['https'],
                        message: 'URL incorrecte',
                    ),
                    new Assert\Regex(
                        pattern: '/^https:\/\/([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,}(\/.*)?$/',
                        message: 'URL incorrecte (ex : https://monsite.com)',
                    ),
                ],
            ])
            // fichier
            ->add('profile_picture_file', FileType::class, [
                'label' => 'Ou uploader une image',
                'mapped' => false,
                'required' => false,
                'constraints' => [
                    new Assert\Image([
                        'maxSize' => '2M',
                        'mimeTypes' => ['image/jpeg', 'image/png', 'image/webp'],
                        'mimeTypesMessage' => 'Format accepté : JPG, PNG ou WebP.',
                    ]),
                ],
                'attr' => ['accept' => 'image/jpeg, image/png, image/webp'],
                'help' => 'PNG, JPG ou WEBP — 2 Mo maximum'
            ])
        ;
    }

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