<?php

declare(strict_types=1);

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

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
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;

/** @extends AbstractType<array<string, mixed>> */
final class SocialPublicationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('channels', ChoiceType::class, [
                'label' => 'social_publishing.form.channels',
                'choices' => $options['channel_choices'],
                'multiple' => true,
                'expanded' => true,
                'constraints' => [new Assert\Count(min: 1)],
            ])
            ->add('message', TextareaType::class, [
                'label' => 'social_publishing.form.message',
                'required' => false,
                'attr' => ['rows' => 8, 'maxlength' => 10_000],
                'constraints' => [new Assert\Length(max: 10_000)],
            ])
            ->add('mediaUrl', UrlType::class, [
                'label' => 'social_publishing.form.media_url',
                'help' => 'social_publishing.form.media_url_help',
                'required' => false,
                'constraints' => [new Assert\Url(protocols: ['https'])],
            ])
            ->add('scheduledAt', DateTimeType::class, [
                'label' => 'social_publishing.form.scheduled_at',
                'widget' => 'single_text',
                'input' => 'datetime_immutable',
                'required' => true,
            ])
            ->add('submit', SubmitType::class, [
                'label' => 'social_publishing.form.submit',
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => null,
            'translation_domain' => 'messages',
            'channel_choices' => [],
        ]);
        $resolver->setAllowedTypes('channel_choices', 'array');
    }
}
