<?php

/*
 * This file is part of the VitiCore 2.
 *
 * (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\Bridge\Symfony\Bundle\Validator\Constraints;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Provider\LocaleProvider;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class TranslationValidator extends ConstraintValidator
{
    public function __construct(
        private readonly LocaleProvider $localeProvider,
    ) {
    }

    public function validate(mixed $value, Constraint $constraint): void
    {
        if (!$constraint instanceof Translation) {
            throw new UnexpectedTypeException($constraint, Translation::class);
        }

        if (null === $value) {
            return;
        }

        foreach ($this->localeProvider->getRequiredLocales() as $locale) {
            if (!$value->containsKey($locale)) {
                $this->context
                    ->buildViolation($constraint->message)
                    ->setParameter('{{ string }}', (string) $locale)
                    ->addViolation();
            }
        }
    }
}
