<?php

namespace App\Form\Modpack;

use App\Entity\Constitue;
use App\Entity\Mod;
use App\Entity\Modpack;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ModpackDownloadType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        /** @var Modpack|null $modpack */
        $modpack = $options['data'] ?? null;

        $optionnels = $modpack?->getConstitues()
            ->filter(fn(Constitue $c) => $c->isOptionnel())
            ->map(fn(Constitue $c) => $c->getMod())
            ->getValues() ?? [];

        $builder
            ->add('mods_optionnels', EntityType::class, [
            'mapped'       => false,
            'class'        => Mod::class,
            'choice_label' => fn(Mod $mod) => $mod->getNom() . ' - ' . $mod->getVersion(),
            'choices'      => $optionnels,
            'multiple'     => true,
            'expanded'     => true,
            'label'        => 'Mods optionnels',
            'required'     => false,
        ]);
    }

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