<?php

declare(strict_types=1);

namespace Akeneo\Category\Application\Command\CreateTemplate;

use Akeneo\Category\Domain\ValueObject\CategoryId;
use Akeneo\Category\Infrastructure\Validation\TemplateCodeShouldBeUnique;
use Symfony\Component\Validator\Constraints;
use Webmozart\Assert\Assert;

/**
 * @copyright 2023 Akeneo SAS (https://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
final class CreateTemplateCommand
{
    #[Constraints\NotBlank]
    #[Constraints\Length(['max' => 100])]
    #[Constraints\Regex(pattern: '/^[a-zA-Z0-9_]+$/', message: 'akeneo.category.validation.template.code.wrong_format')]
    #[TemplateCodeShouldBeUnique]
    public readonly string $templateCode;

    /**
     * @var array<string,string|null>
     */
    #[Constraints\All([
        new Constraints\Length(['max' => 255]),
    ])]
    public readonly array $labels;

    /**
     * @param array{code:string,labels:array<string,string>} $data
     */
    public function __construct(
        public readonly CategoryId $categoryTreeId,
        array $data,
    ) {
        Assert::string($data['code']);
        Assert::isMap($data['labels']);
        Assert::allNullOrString($data['labels']);

        $this->templateCode = $data['code'];
        $this->labels = $data['labels'];
    }
}
