<?php

namespace App\Form\Properties;

use App\Entity\Comptabilites\Currencies\Currencies;
use App\Entity\Parametres\Parametres;
use App\Entity\Properties\Chargescautions;
use App\Form\Types\NumberInputType;
use App\Form\Types\TextInputType;
use App\Repository\Parametres\ParametresRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ChargescautionsType 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();

        // 🔹 Récupération de l'ID société
        $appId = intval($this->container->getParameter('company_id'));
        $app = $this->parametresRepository->find($appId);

        // 🔹 Définition d'une devise par défaut
        $defaultCurrencyName = 'Franc CFA';
        $defaultCurrencyCode = 'XOF';

        // 🔹 Récupération de la devise de l'application si possible
        if ($app instanceof Parametres && $app->getCurrencies() instanceof Currencies) {
            $currencyName = $app->getCurrencies()->getCurrencyName();
            $currencyCode = $app->getCurrencies()->getCurrencyCode();
        } else {
            // Si aucune donnée n’est trouvée, utiliser la devise par défaut
            $currencyName = $defaultCurrencyName;
            $currencyCode = $defaultCurrencyCode;
        }

        $builder
            ->add('name', TextInputType::class, [
                'label' => 'Libéllé <span class="text-danger">*</span>',
                'label_html' => true,
                'icon' => 'icon-file-text',
                'color' => null,
                'attr' => [
                    'placeholder' => 'Libéllé *'
                ]
            ])
            ->add('montant', MoneyType::class, [
                'label' => 'Montant en ' . $currency->getCurrencyName(),
                'currency' => $currency->getCurrencyCode(),
                'required' => true,
                'attr' => [
                    'placeholder' => 'Montant en ' . $currency->getCurrencyName()
                ]
            ])
            ->add('taux', NumberInputType::class, [
                'label' => 'Taux (Qté)',
                'icon' => 'icon-file-text',
                'color' => null,
                'attr' => [
                    'placeholder' => 'Taux (Qté)',
                    'min' => 1,
                ]
            ])
        ;
    }

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