<?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;

use Ferienpass\AdminBundle\Form\CompoundType\TenantDomainsType;
use Ferienpass\CoreBundle\Entity\Tenant;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class EditTenantType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Tenant::class,
            'label_format' => 'tenants.label.%name%',
            'translation_domain' => 'admin',
            'required' => true,
        ]);
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $tenant = $builder->getData();

        $builder
            ->add('name', TextType::class, ['section' => 'base', 'width' => '1/2'])
            ->add('slug', TextType::class, [
                'section' => 'base',
                'width' => '1/2',
                'help' => 'tenants.help.slug',
                // The slug ends up in console options, log lines and cache keys.
                // Changing it later would orphan all of those, so it is set once.
                'disabled' => $tenant instanceof Tenant && null !== $tenant->getId(),
            ])
            ->add('domains', TenantDomainsType::class, ['section' => 'domains', 'label' => false])
            ->add('disabled', CheckboxType::class, [
                'section' => 'settings',
                'required' => false,
                'help' => 'tenants.help.disabled',
            ])
        ;
    }
}
