<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Form\Admin;

use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\AbandonedCartConfigurationType;
use CpCreation\VitiCore\Marketing\Model\Translation\AbandonedCartConfigurationTranslation;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormBuilderInterface;

final class AbandonedCartConfigurationTypeTest extends TestCase
{
    public function testLocalizedEmailFieldsExposeLimitsAndCharacterCounters(): void
    {
        $translationsType = null;
        $translationsOptions = null;
        $builder = $this->createMock(FormBuilderInterface::class);
        $builder->method('add')
            ->willReturnCallback(
                static function (mixed $child, ?string $type = null, array $options = []) use (&$translationsType, &$translationsOptions, $builder): FormBuilderInterface {
                    if ('translations' === $child) {
                        $translationsType = $type;
                        $translationsOptions = $options;
                    }

                    return $builder;
                }
            );

        new AbandonedCartConfigurationType()->buildForm($builder, []);

        self::assertSame(TranslationsType::class, $translationsType);
        self::assertIsArray($translationsOptions);

        $title = $translationsOptions['fields']['emailTitle'];
        self::assertFalse($title['required']);

        $subject = $translationsOptions['fields']['emailSubject'];
        self::assertSame(
            AbandonedCartConfigurationTranslation::SUBJECT_MAX_LENGTH,
            $subject['attr']['maxlength'],
        );
        self::assertTrue($subject['attr']['data-character-counter']);
        self::assertFalse($subject['required']);

        $preheader = $translationsOptions['fields']['emailPreheader'];
        self::assertSame(
            AbandonedCartConfigurationTranslation::PREHEADER_MAX_LENGTH,
            $preheader['attr']['maxlength'],
        );
        self::assertTrue($preheader['attr']['data-character-counter']);
        self::assertFalse($preheader['required']);
        self::assertSame(['fr'], $translationsOptions['required_locales']);
    }
}
