<?php

namespace App\Form\Properties;

use App\Entity\Properties\Charge;
use App\Form\Types\TextInputType;
use Doctrine\ORM\EntityRepository;
use App\Form\Types\NumberInputType;
use App\Entity\Parametres\Parametres;
use App\Entity\Properties\Properties;
use Symfony\Component\Form\AbstractType;
use Doctrine\DBAL\Types\DateIntervalType;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use App\Entity\Comptabilites\Currencies\Currencies;
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\DependencyInjection\ContainerInterface;

class ChargesType 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();
        $appId = intval($this->container->getParameter('company_id'));

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

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

        $currency = $app->getCurrencies();

        if ($currency) {
            $currencyCode = $currency->getCurrencyCode();
            $currencyName = $currency->getCurrencyName();
        } else {
            $currencyCode = 'XOF'; // Valeur par défaut
            $currencyName = 'FCFA';
        }


        $builder
            ->add('properties', EntityType::class, [
                'label' => 'Appartements <span class="text-danger">*</span>',
                'label_html' => true,
                'required' => true,
                'class' => Properties::class,
                'placeholder' => 'Aucun appartement(proprieté) 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.createdAt', 'DESC');
                },
                'attr' => ['class' => 'select-search']
            ])
            ->add('montant', MoneyType::class, [
                'label' => 'Montant en ' . $currencyName,           // ← $currencyName (pas $currency)
                'currency' => $currencyCode,                        // ← $currencyCode (pas $currency)
                'required' => true,
                'attr' => [
                    'placeholder' => 'Montant en ' . $currencyName   // ← $currencyName (pas $currency)
                ]
            ])
            ->add('type', TextInputType::class, [
                'label' => "Type d'intervention <span class='text-danger'>*</span>",
                'icon' => 'icon-file-text',
                'color' => null,
                'required' => true,
                'attr' => [
                    'placeholder' => "Type d'intervention",

                ]
            ])

            ->add('date_intervention', DateType::class, [
                'label' => "Date d'intervention <span class='text-danger'>*</span>",
                'label_html' => true,
                'required' => true,
                'widget' => 'single_text', // IMPORTANT pour avoir un input type="date"
                'attr' => [
                    'placeholder' => "Date d'intervention",
                ],
            ])
            ->add('description', DateIntervalType::class, [
                'label' => "Description",
                'icon' => 'icon-file-text',
                'color' => null,
                'required' => true,
                'attr' => [
                    'placeholder' => "description",

                ]
            ])
        ;
    }

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