<?php

namespace App\Form\Properties;

use DateTime;
use App\Form\Types\SwitchType;
use App\Form\Types\TextInputType;
use App\Entity\Acteurs\Locataires;
use Doctrine\ORM\EntityRepository;
use App\Form\Types\NumberInputType;
use App\Entity\Acteurs\Proprietaires;
use App\Entity\Parametres\Parametres;
use App\Entity\Properties\Categories;
use App\Entity\Properties\Properties;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Security\Core\Security;
use App\Entity\Parametres\Locations\Locations;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use App\Entity\Comptabilites\Currencies\Currencies;
use App\Entity\Properties\Bien;
use App\Entity\Properties\Demarcheur;
use App\Repository\Parametres\ParametresRepository;
use Symfony\Component\OptionsResolver\OptionsResolver;
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\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

class BienType extends AbstractType
{

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




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

        $user = $this->security->getUser();
        $isSuperAdmin = in_array('ROLE_SUPER_ADMIN', $user->getRoles());
        $companyId = $user->getCompanyId();

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

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



        $builder
            ->add('demarcheur', EntityType::class, [
                'label' => 'Démarcheur <span class="text-danger">*</span>',
                'label_html' => true,
                'class' => Demarcheur::class,
                'required' => true,
                'placeholder' => 'Aucun type sélectionné',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('c')
                        ->andWhere('c.active = TRUE')
                        // ->andWhere('c.deletedAt IS NULL')
                        ->orderBy('c.nom', '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 <span class="text-danger">*</span>',
                'label_html' => true,
                'required' => true,
                'class' => Locations::class,
                'placeholder' => 'Aucun lieu sélectionné',
                'query_builder' => function (EntityRepository $er) use ($isSuperAdmin, $companyId) {
                    $qb = $er->createQueryBuilder('l')
                        ->andWhere('l.is_actif = TRUE')
                        ->andWhere('l.deletedAt IS NULL');

                    // if (!$isSuperAdmin) {
                    //     $qb->andWhere('l.companyId  = :company_id')
                    //         ->setParameter('company_id', $companyId);
                    // }

                    return $qb->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) use ($isSuperAdmin, $companyId) {
                    $qb = $er->createQueryBuilder('p')
                        ->andWhere('p.is_actif = TRUE')
                        ->andWhere('p.deletedAt IS NULL');

                    // if (!$isSuperAdmin) {
                    //     $qb->andWhere('p.companyId  = :company_id')
                    //         ->setParameter('company_id', $companyId);
                    // }

                    return $qb->orderBy('p.name', 'ASC');
                },
                'attr' => ['class' => 'select-search']
            ])
            ->add('name', TextInputType::class, [
                'label' => 'Nom du bien <span class="text-danger">*</span>',
                'label_html' => true,
                'required' => true,
                'icon' => 'icon-user',
                'color' => null,
                'attr' => [
                    'placeholder' => 'Nom du bien *'
                ]
            ])
            ->add('adresse', TextInputType::class, [
                'label' => 'Adresse du bien',
                'label_html' => true,
                'required' => false,
                'icon' => 'icon-sort-numeric-asc',
                'color' => null,
                'attr' => [
                    'placeholder' => 'Adresse du bien'
                ]
            ])
            ->add('commission', NumberInputType::class, [
                'required' => false,
                'label' => 'Commission (%)',
                'icon' => 'icon-sort-numeric-asc',
                'color' => null,
                'attr' => [
                    'placeholder' => 'Commission (%)',
                    'min' => 0,
                ]
            ])

            ->add('active', SwitchType::class, ['label' => 'Bien active?', 'required' => false])
        ;
    }

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