<?php

namespace App\Dto\Input;

use ApiPlatform\Metadata\ApiProperty;
use App\Entity\PxeTemplate;
use App\Validator\Constraints\PxeTemplateUniqueDefault;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

#[PxeTemplateUniqueDefault]
final class PxeTemplateInput
{
    #[Assert\NotBlank(message: 'validators.pxe_template.name.not_blank')]
    #[Groups(['pxe-template:write'])]
    #[ApiProperty(description: 'The name of the pxeTemplate', example: "PxeTemplate 1")]
    public ?string $name = null;

    #[Groups(['pxe-template:write'])]
    #[ApiProperty(description: 'The content of the pxeTemplate', example: "content of the pxeTemplate 1")]
    public ?string $templateContent = null;

    #[Groups(['pxe-template:write'])]
    #[ApiProperty(description: 'The default pxeTemplate', example: "true")]
    public ?bool $isDefault = null;

    public function __construct(?PxeTemplate $pxeTemplate = null)
    {
        if (!$pxeTemplate) {
            return;
        }

        $this->name = $pxeTemplate->getName();
        $this->templateContent = $pxeTemplate->getTemplateContent();
        $this->isDefault = $pxeTemplate->isDefault();
    }

    public function createOrUpdateEntity(?PxeTemplate $pxeTemplate = null): PxeTemplate
    {
        if (!$pxeTemplate) {
            $pxeTemplate = new PxeTemplate();
        }

        $pxeTemplate->setName($this->name);
        $pxeTemplate->setTemplateContent($this->templateContent);
        $pxeTemplate->setDefault($this->isDefault);

        return $pxeTemplate;
    }
}