<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

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

use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use CpCreation\VitiCore\Marketing\Model\ReviewRequestConfiguration;
use CpCreation\VitiCore\Marketing\Model\Translation\ReviewRequestConfigurationTranslation;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
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\Security\Core\Authorization\AuthorizationCheckerInterface;

final class ReviewRequestConfigurationType extends AbstractType
{
    public function __construct(
        private readonly AuthorizationCheckerInterface $authorizationChecker,
    ) {}

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('enabled', CheckboxType::class, [
                'label' => 'marketing.review_request.configuration.enabled',
                'help' => 'marketing.review_request.configuration.enabled_help',
                'required' => false,
            ])
            ->add('sendDelayDays', IntegerType::class, [
                'label' => 'marketing.review_request.configuration.send_delay_days',
                'help' => 'marketing.review_request.configuration.send_delay_days_help',
                'attr' => ['min' => 1, 'max' => 365],
            ])
            ->add('tokenLifetimeDays', IntegerType::class, [
                'label' => 'marketing.review_request.configuration.token_lifetime_days',
                'help' => 'marketing.review_request.configuration.token_lifetime_days_help',
                'attr' => ['min' => 1, 'max' => 365],
            ])
            ->add('secondReminderEnabled', CheckboxType::class, [
                'label' => 'marketing.review_request.configuration.second_reminder_enabled',
                'help' => 'marketing.review_request.configuration.second_reminder_enabled_help',
                'required' => false,
            ])
            ->add('secondReminderDelayDays', IntegerType::class, [
                'label' => 'marketing.review_request.configuration.second_reminder_delay_days',
                'help' => 'marketing.review_request.configuration.second_reminder_delay_days_help',
                'attr' => ['min' => 1, 'max' => 365],
            ])
            ->add('moderationRequired', CheckboxType::class, [
                'label' => 'marketing.review_request.configuration.moderation_required',
                'help' => 'marketing.review_request.configuration.moderation_required_help',
                'required' => false,
            ])
            ->add('googleBusinessReviewUrl', UrlType::class, [
                'label' => 'marketing.review_request.configuration.google_business_review_url',
                'help' => 'marketing.review_request.configuration.google_business_review_url_help',
                'required' => false,
                'default_protocol' => 'https',
                'attr' => [
                    'autocomplete' => 'off',
                    'placeholder' => 'https://g.page/r/…/review',
                ],
            ])
            ->add('fulfilledAfter', DateTimeType::class, [
                'label' => 'marketing.review_request.configuration.fulfilled_after',
                'help' => 'marketing.review_request.configuration.fulfilled_after_help',
                'input' => 'datetime_immutable',
                'widget' => 'single_text',
            ]);

        $translationFields = [
            'emailTitle' => [
                'field_type' => TextType::class,
                'label' => 'marketing.review_request.configuration.email_title',
                'help' => 'marketing.review_request.configuration.email_title_help',
                'help_translation_parameters' => [
                    '%max%' => ReviewRequestConfigurationTranslation::TITLE_MAX_LENGTH,
                ],
                'required' => false,
                'attr' => [
                    'maxlength' => ReviewRequestConfigurationTranslation::TITLE_MAX_LENGTH,
                    'data-character-counter' => true,
                    'autocomplete' => 'off',
                ],
            ],
        ];

        if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
            $translationFields['policyAdditionalContent'] = [
                'field_type' => TrumbowygType::class,
                'label' => 'marketing.review_request.configuration.policy_additional_content',
                'help' => 'marketing.review_request.configuration.policy_additional_content_help',
                'required' => false,
            ];
        }

        $builder->add('translations', TranslationsType::class, [
            'label' => false,
            'fields' => $translationFields,
            'required_locales' => ['fr'],
        ]);
    }

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