<?php

declare(strict_types=1);

namespace MauticPlugin\DealFlowBundle\Form\Type;

use MauticPlugin\DealFlowBundle\Service\AutomationOptionsProvider;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;

final class CampaignDealCreateActionType extends AbstractType
{
    public function __construct(
        private readonly AutomationOptionsProvider $optionsProvider,
    ) {
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('title', TextType::class, [
                'label' => 'Deal title',
                'required' => true,
                'attr' => [
                    'class' => 'form-control',
                    'placeholder' => 'e.g. {contactfield=company} - {contactfield=firstname} {contactfield=lastname}',
                ],
                'constraints' => [
                    new NotBlank(['message' => 'mautic.core.value.required']),
                ],
            ])
            ->add('pipeline_id', ChoiceType::class, [
                'label' => 'Pipeline',
                'choices' => $this->optionsProvider->getPipelineChoices(),
                'required' => false,
                'placeholder' => 'Default pipeline',
                'choice_translation_domain' => false,
                'attr' => ['class' => 'form-control'],
            ])
            ->add('stage_id', ChoiceType::class, [
                'label' => 'Initial stage',
                'choices' => $this->optionsProvider->getGroupedStageChoices(),
                'required' => false,
                'placeholder' => 'First stage in pipeline',
                'choice_translation_domain' => false,
                'attr' => ['class' => 'form-control'],
            ])
            ->add('amount', NumberType::class, [
                'label' => 'Deal amount',
                'required' => false,
                'scale' => 2,
                'attr' => [
                    'class' => 'form-control',
                    'step' => '0.01',
                ],
            ])
            ->add('status', ChoiceType::class, [
                'label' => 'Deal status',
                'choices' => [
                    'Open' => 'open',
                    'Closed won' => 'won',
                    'Closed lost' => 'lost',
                ],
                'required' => false,
                'data' => 'open',
                'choice_translation_domain' => false,
                'attr' => ['class' => 'form-control'],
            ]);
    }

    public function getBlockPrefix(): string
    {
        return 'dealflow_campaign_create_action';
    }
}
