<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\AdminBundle\Form\CompoundType;

use Ferienpass\CoreBundle\Entity\Tenant;
use Ferienpass\CoreBundle\Entity\TenantDomain;
use Ferienpass\CoreBundle\Tenant\Surface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class TenantDomainType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add('host', TextType::class, [
            'label' => false,
            'attr' => ['placeholder' => 'ferienpass.musterstadt.de'],
        ]);

        // The surface explanation lives in the section legend, not in a `help`
        // here — the collection repeats every entry option per domain, and four
        // copies of the same paragraph drown out the form.
        $builder->add('surface', EnumType::class, [
            'class' => Surface::class,
            'label' => false,
            'choice_label' => static fn (Surface $surface): string => $surface->label(),
            'choice_translation_domain' => 'admin',
        ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => TenantDomain::class,
            'empty_data' => static function (FormInterface $form): TenantDomain {
                $tenant = $form->getParent()?->getParent()?->getData();

                return new TenantDomain($tenant instanceof Tenant ? $tenant : null);
            },
        ]);
    }
}
