<?php

namespace App\Form\Properties;

use App\Entity\Acteurs\Locataires;
use App\Entity\Acteurs\Proprietaires;
use App\Entity\Comptabilites\Currencies\Currencies;
use App\Entity\Parametres\Locations\Locations;
use App\Entity\Parametres\Parametres;
use App\Entity\Properties\Categories;
use App\Entity\Properties\Properties;
use App\Form\Types\NumberInputType;
use App\Form\Types\SwitchType;
use App\Form\Types\TextInputType;
use App\Repository\Parametres\ParametresRepository;
use DateTime;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PropertiesType extends AbstractType
{

    public function __construct(
        private ParametresRepository $parametresRepository,
        private ContainerInterface $container
    ) {}

    /**
     * @param FormBuilderInterface<Callable> $builder
     * @param array[] $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {

        $appId = intval($this->container->getParameter('company_id'));

        /** @var Parametres $app */
        $app = $this->parametresRepository->find($appId);

        /** @var Currencies $currency */
        $currency = $app?->getCurrencies();

        $builder
            ->add('categories', EntityType::class, [
                'label' => 'Type de propriété <span class="text-danger">*</span>',
                'label_html' => true,
                'class' => Categories::class,
                'required' => true,
                'placeholder' => 'Aucun type sélectionné',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('c')
                        ->andWhere('c.is_actif = TRUE')
                        ->andWhere('c.deletedAt IS NULL')
                        ->orderBy('c.name', 'ASC')
                    ;
                },
                'attr' => [
                    'class' => 'select-search'
                ]
            ])
            ->add('locataires', EntityType::class, [
                'label' => 'Locataires <span class="text-danger">*</span>',
                'label_html' => true,
                'required' => true,
                'class' => Locataires::class,
                'placeholder' => 'Aucun locataire sélectionné',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('l')
                        ->andWhere('l.is_actif = TRUE')
                        ->andWhere('l.is_out = FALSE')
                        ->andWhere('l.deletedAt IS NULL')
                        ->orderBy('l.name', 'ASC')
                    ;
                },
                'attr' => [
                    'class' => 'select-search'
                ]
            ])
            ->add('locations', EntityType::class, [
                'label' => 'Lieu de la propriété <span class="text-danger">*</span>',
                'label_html' => true,
                'required' => true,
                'class' => Locations::class,
                'placeholder' => 'Aucun lieu sélectionné',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('l')
                        ->andWhere('l.is_actif = TRUE')
                        ->andWhere('l.deletedAt IS NULL')
                        ->orderBy('l.name', 'ASC')
                    ;
                },
                'attr' => [
                    'class' => 'select-search'
                ]
            ])
            ->add('proprietaires', EntityType::class, [
                'label' => 'Propriétaires <span class="text-danger">*</span>',
                'label_html' => true,
                'required' => true,
                'class' => Proprietaires::class,
                'placeholder' => 'Aucun propriétaire sélectionné',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('p')
                        ->andWhere('p.is_actif = TRUE')
                        ->andWhere('p.deletedAt IS NULL')
                        ->orderBy('p.name', 'ASC')
                    ;
                },
                'attr' => [
                    'class' => 'select-search'
                ]
            ])
            ->add('name', TextInputType::class, [
                'label' => 'Nom de propriété <span class="text-danger">*</span>',
                'label_html' => true,
                'required' => true,
                'icon' => 'icon-user',
                'color' => null,
                'attr' => [
                    'placeholder' => 'Nom de propriété *'
                ]
            ])
            ->add('numAppartement', TextInputType::class, [
                'label' => 'N° de la propriété',
                'label_html' => true,
                'required' => false,
                'icon' => 'icon-sort-numeric-asc',
                'color' => null,
                'attr' => [
                    'placeholder' => 'N° de la propriété'
                ]
            ])
            ->add('delaisLoyer', NumberInputType::class, [
                'required' => false,
                'label' => 'Nombre de jour (date limite du loyer)',
                'icon' => 'icon-sort-numeric-asc',
                'color' => null,
                'attr' => [
                    'placeholder' => 'Nombre de jour (date limite du loyer)'
                ]
            ])
            ->add('montantLoyer', MoneyType::class, [
                'label' => 'Montant du loyer mensuel en ' . $currency?->getCurrencyName() . ' <span class="text-danger">*</span>',
                'label_html' => true,
                'currency' => $currency->getCurrencyCode(),
                'required' => true,
                'attr' => [
                    'placeholder' => 'Montant du loyer mensuel en ' . $currency?->getCurrencyName()
                ]
            ])
            ->add('adresse', TextareaType::class, [
                'label' => 'Adresse de la propriété',
                'required' => false,
                'attr' => [
                    'placeholder' => 'Adresse de la propriété'
                ]
            ])
            ->add('cautionGestionnaire', TextInputType::class, [
                'label' => 'Nom du gestionnaire',
                'icon' => 'icon-user',
                'color' => null,
                'required' => false,
                'attr' => [
                    'placeholder' => 'Nom du gestionnaire'
                ]
            ])
            ->add('cautionLibelle', TextInputType::class, [
                'label' => 'Libéllé ou désignation',
                'icon' => 'icon-user',
                'color' => null,
                'required' => false,
                'attr' => [
                    'placeholder' => 'Libéllé ou désignation'
                ]
            ])
            ->add('cautionPeriode', NumberInputType::class, [
                'label' => 'Caution (Avance nbre de mois)',
                'icon' => 'icon-user',
                'color' => null,
                'required' => false,
                'attr' => [
                    'placeholder' => 'Caution (Avance nbre de mois)',
                    'min' => 0
                ]
            ])
            ->add('cautionMois', NumberInputType::class, [
                'label' => 'Caution (Nbre de mois)',
                'icon' => 'icon-user',
                'color' => null,
                'required' => false,
                'attr' => [
                    'placeholder' => 'Caution (Nbre de mois)',
                    'min' => 0
                ]
            ])
            ->add('cautionDate', DateType::class, [
                'required' => false,
                'html5' => true,
                'input_format' => 'dd/MM/yyyy',
                'widget' => 'single_text',
                'label' => 'Date d\'entrée du locataire',
                /*'data' => new DateTime('now'),*/
            ])
            ->add('cautionMontant', MoneyType::class, [
                'label' => 'Montant de la caution en ' . $currency?->getCurrencyName(),
                'label_html' => true,
                'currency' => $currency->getCurrencyCode(),
                'required' => false,
                'attr' => [
                    'placeholder' => 'Montant de la caution en ' . $currency?->getCurrencyName()
                ]
            ])
            ->add('documents', CollectionType::class, [
                'required' => false,
                'label' => false,
                'entry_type' => DocumentsType::class,
                'by_reference' => false,
                'allow_add' => true,
                'allow_delete' => true,
                'attr' => [],
            ])
            ->add('chargescautions', CollectionType::class, [
                'required' => false,
                'label' => false,
                'entry_type' => ChargescautionsType::class,
                'by_reference' => false,
                'allow_add' => true,
                'allow_delete' => true,
                'attr' => [],
            ])
            ->add('isActif', SwitchType::class, ['label' => 'Propriété active?', 'required' => false])
        ;
    }

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