<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Tests\Form\Admin;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\ContactType;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

final class ContactTypeTest extends TestCase
{
    /**
     * @param list<string> $expectedFields
     * @param list<string> $unexpectedFields
     */
    #[DataProvider('roleProvider')]
    public function testLegalContentFieldsAreRestrictedToSuperAdministrators(
        bool $isSuperAdmin,
        array $expectedFields,
        array $unexpectedFields,
    ): void {
        $authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
        $authorizationChecker->expects(self::once())
            ->method('isGranted')
            ->with('ROLE_SUPER_ADMIN')
            ->willReturn($isSuperAdmin);

        $fields = [];
        $builder = $this->createMock(FormBuilderInterface::class);
        $builder->method('add')
            ->willReturnCallback(static function (string $child) use (&$fields, $builder): FormBuilderInterface {
                $fields[] = $child;

                return $builder;
            });

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

        foreach ($expectedFields as $field) {
            self::assertContains($field, $fields);
        }

        foreach ($unexpectedFields as $field) {
            self::assertNotContains($field, $fields);
        }
    }

    /**
     * @return iterable<string, array{bool, list<string>, list<string>}>
     */
    public static function roleProvider(): iterable
    {
        $legalFields = ['legalNoticeContent', 'privacyPolicyContent'];

        yield 'super administrateur' => [true, $legalFields, []];
        yield 'administrateur' => [false, [], $legalFields];
    }

    public function testLocalBusinessFieldsAreAvailable(): void
    {
        $authorizationChecker = $this->createStub(AuthorizationCheckerInterface::class);
        $fields = [];
        $builder = $this->createMock(FormBuilderInterface::class);
        $builder->method('add')
            ->willReturnCallback(static function (string $child) use (&$fields, $builder): FormBuilderInterface {
                $fields[] = $child;

                return $builder;
            });

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

        foreach (['country', 'latitude', 'longitude', 'openingHours', 'image'] as $field) {
            self::assertContains($field, $fields);
        }
    }
}
