<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\MainBundle\Form\Type;

use Chill\MainBundle\Notification\NotificationFlagManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class NotificationFlagsType extends AbstractType
{
    private readonly array $notificationFlagProviders;

    public function __construct(NotificationFlagManager $notificationFlagManager)
    {
        $this->notificationFlagProviders = $notificationFlagManager->getAllNotificationFlagProviders();
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        foreach ($this->notificationFlagProviders as $flagProvider) {
            $flag = $flagProvider->getFlag();
            $flagBuilder = $builder->create($flag, options: [
                'label' => $flagProvider->getLabel(),
                'compound' => true,
            ]);

            $flagBuilder
                ->add('immediate_email', CheckboxType::class, [
                    'label' => false,
                    'required' => false,
                ])
                ->add('daily_digest', CheckboxType::class, [
                    'label' => false,
                    'required' => false,
                ])
            ;
            $builder->add($flagBuilder);
        }
    }

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