<?php

/*
 * 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 CpCreation\VitiCore\Contact\Model\Contact;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
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 ContactType extends AbstractType
{
    public function __construct(private readonly AuthorizationCheckerInterface $authorizationChecker) {}

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $autocompleteOff = ['autocomplete' => 'new-field'];

        $builder
            ->add('company', TextType::class, [
                'label' => 'Nom de l\'entreprise',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('firstname', TextType::class, [
                'label' => 'Nom du contact',
                'required' => false,
                'attr' => $autocompleteOff,
            ])
            ->add('lastname', TextType::class, [
                'label' => 'Prénom du contact',
                'required' => false,
                'attr' => $autocompleteOff,
            ])
            ->add('address', TextType::class, [
                'label' => 'Rue',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('postal', TextType::class, [
                'label' => 'Code postal',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('city', TextType::class, [
                'label' => 'Ville',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('country', CountryType::class, [
                'label' => 'Pays',
                'required' => true,
                'preferred_choices' => ['FR'],
            ])
            ->add('latitude', NumberType::class, [
                'label' => 'Latitude GPS',
                'required' => false,
                'scale' => 7,
                'html5' => true,
                'input' => 'string',
                'attr' => [
                    ...$autocompleteOff,
                    'min' => -90,
                    'max' => 90,
                    'step' => '0.0000001',
                    'placeholder' => '49.0434',
                ],
            ])
            ->add('longitude', NumberType::class, [
                'label' => 'Longitude GPS',
                'required' => false,
                'scale' => 7,
                'html5' => true,
                'input' => 'string',
                'attr' => [
                    ...$autocompleteOff,
                    'min' => -180,
                    'max' => 180,
                    'step' => '0.0000001',
                    'placeholder' => '3.9560',
                ],
            ])
            ->add('openingHours', CollectionType::class, [
                'label' => 'Horaires d’ouverture',
                'entry_type' => LocalBusinessOpeningHoursType::class,
                'entry_options' => ['label' => false],
                'allow_add' => true,
                'allow_delete' => true,
                'delete_empty' => true,
                'prototype' => true,
                'error_bubbling' => false,
                'attr' => [
                    'class' => 'local-business-opening-hours-collection',
                ],
                'help' => 'Ajoutez une plage par groupe de jours ayant les mêmes horaires.',
            ])
            ->add('phone1', TextType::class, [
                'label' => 'Téléphone 1',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('phone2', TextType::class, [
                'label' => 'Téléphone 2',
                'required' => false,
                'attr' => $autocompleteOff,
            ])
            ->add('phone3', TextType::class, [
                'label' => 'Téléphone 3',
                'required' => false,
                'attr' => $autocompleteOff,
            ])
            ->add('emailFrom', EmailType::class, [
                'label' => 'Email d\'envoi',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('emailToMessage', EmailType::class, [
                'label' => 'Email réception message contact',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('emailToShop', EmailType::class, [
                'label' => 'Email réception commande',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('facebook', UrlType::class, [
                'label' => 'Facebook',
                'required' => false,
                'attr' => $autocompleteOff,
            ])
            ->add('twitter', UrlType::class, [
                'label' => 'Twitter',
                'required' => false,
                'attr' => $autocompleteOff,
            ])
            ->add('instagram', UrlType::class, [
                'label' => 'Instagram',
                'required' => false,
                'attr' => $autocompleteOff,
            ])

            // Types custom : garde-les tels quels, mais pense à ajouter les use si besoin
            ->add('cgv', TrumbowygType::class, [
                'label' => 'CGV',
                'required' => false,
            ])
            ->add('legalNoticeExtraParagraph', TrumbowygType::class, [
                'label' => 'Paragraphe en bas des mentions légales',
                'required' => false,
            ])
            ->add('privacyPolicyExtraParagraph', TrumbowygType::class, [
                'label' => 'Paragraphe en bas de la politique de confidentialité',
                'required' => false,
            ])
            ->add('committeeChampagneSiret', TextType::class, [
                'label' => 'Ressortissant du Comité Champagne (SIRET)',
                'required' => false,
            ])
            ->add('committeeChampagneIdentifiant', TextType::class, [
                'label' => 'Identifiant unique fourni par l\'ADEME (FRXXX)',
                'required' => false,
            ])
            ->add('image', ImageType::class, [
                'label' => 'Logo',
                'required' => false,
            ])
            ->add('editorCompany', TextType::class, [
                'label' => 'Dénomination de la société',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('editorAddress', TextType::class, [
                'label' => 'Adresse',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('editorPostal', TextType::class, [
                'label' => 'Code postal',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('editorCity', TextType::class, [
                'label' => 'Ville',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('editorRcs', TextType::class, [
                'label' => 'RCS',
                'required' => true,
                'attr' => $autocompleteOff,
            ])
            ->add('editorTva', TextType::class, [
                'label' => 'TVA intra.',
                'required' => true,
                'attr' => $autocompleteOff,
            ]);

        if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
            $builder
                ->add('legalNoticeContent', TrumbowygType::class, [
                    'label' => 'Contenu principal des mentions légales',
                    'required' => false,
                    'help' => 'Réservé aux super administrateurs.',
                ])
                ->add('privacyPolicyContent', TrumbowygType::class, [
                    'label' => 'Contenu principal de la politique de confidentialité',
                    'required' => false,
                    'help' => 'Réservé aux super administrateurs.',
                ]);
        }
    }

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