<?php

declare(strict_types=1);

namespace MauticPlugin\DealFlowBundle\Form\Type;

use Mautic\LeadBundle\Form\Type\HtmlType;
use MauticPlugin\DealFlowBundle\Entity\DealTask;
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\DateType;
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 DealTaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('dealId', ChoiceType::class, [
                'label' => 'Deal',
                'choices' => $options['deal_choices'],
                'placeholder' => 'Select a deal',
            ])
            ->add('title', TextType::class, [
                'label' => 'Task title',
                'constraints' => [
                    new Assert\NotBlank(message: 'Task title is required.'),
                ],
                'attr' => [
                    'class' => 'form-control',
                    'placeholder' => 'Follow up on pricing review',
                    'required' => true,
                ],
            ])
            ->add('priority', ChoiceType::class, [
                'label' => 'Priority',
                'choices' => [
                    'Normal' => DealTask::PRIORITY_NORMAL,
                    'Urgent' => DealTask::PRIORITY_URGENT,
                    'High' => DealTask::PRIORITY_HIGH,
                    'Low' => DealTask::PRIORITY_LOW,
                ],
            ])
            ->add('ownerId', ChoiceType::class, [
                'label' => 'Owner',
                'choices' => $options['owner_choices'],
                'placeholder' => 'No owner assigned',
                'required' => false,
            ])
            ->add('dueAt', DateType::class, [
                'label' => 'Due date',
                'required' => false,
                'widget' => 'single_text',
                'input' => 'string',
                'attr' => [
                    'class' => 'form-control',
                ],
            ])
            ->add('reminderAt', DateTimeType::class, [
                'label' => 'Reminder',
                'required' => false,
                'widget' => 'single_text',
                'input' => 'string',
                'attr' => [
                    'class' => 'form-control',
                ],
            ])
            ->add('description', HtmlType::class, [
                'label' => 'Task details',
                'required' => false,
                'attr' => [
                    'class' => 'editor editor-basic',
                    'rows' => 8,
                    'placeholder' => 'Capture context, expected outcome, or the exact follow-up needed.',
                ],
            ]);
    }

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