<?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\Tests\Validation\Validator;

use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\MainBundle\Validation\Validator\UserGroupDoNotExclude;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

/**
 * @internal
 *
 * @coversNothing
 */
class UserGroupDoNotExcludeTest extends ConstraintValidatorTestCase
{
    protected function createValidator()
    {
        return new UserGroupDoNotExclude(
            new class () implements TranslatableStringHelperInterface {
                public function localize(array $translatableStrings): ?string
                {
                    return $translatableStrings['fr'];
                }
            }
        );
    }

    public function testEmptyArrayIsValid(): void
    {
        $this->validator->validate([], new \Chill\MainBundle\Validation\Constraint\UserGroupDoNotExclude());

        $this->assertNoViolation();
    }

    public function testMixedUserGroupAndUsersIsValid(): void
    {
        $this->validator->validate(
            [new User(), new UserGroup()],
            new \Chill\MainBundle\Validation\Constraint\UserGroupDoNotExclude()
        );

        $this->assertNoViolation();
    }

    public function testDifferentExcludeKeysIsValid(): void
    {
        $this->validator->validate(
            [(new UserGroup())->setExcludeKey('A'), (new UserGroup())->setExcludeKey('B')],
            new \Chill\MainBundle\Validation\Constraint\UserGroupDoNotExclude()
        );

        $this->assertNoViolation();
    }

    public function testMultipleGroupsWithEmptyExcludeKeyIsValid(): void
    {
        $this->validator->validate(
            [(new UserGroup())->setExcludeKey(''), (new UserGroup())->setExcludeKey('')],
            new \Chill\MainBundle\Validation\Constraint\UserGroupDoNotExclude()
        );

        $this->assertNoViolation();
    }

    public function testSameExclusionKeyWillRaiseError(): void
    {
        $this->validator->validate(
            [
                (new UserGroup())->setExcludeKey('A')->setLabel(['fr' => 'Group 1']),
                (new UserGroup())->setExcludeKey('A')->setLabel(['fr' => 'Group 2']),
            ],
            new \Chill\MainBundle\Validation\Constraint\UserGroupDoNotExclude()
        );

        $this->buildViolation('The groups {{ excluded_groups }} do exclude themselves. Please choose one between them')
            ->setParameter('excluded_groups', 'Group 1, Group 2')
            ->setCode('e16c8226-0090-11ef-8560-f7239594db09')
            ->assertRaised();
    }
}
