<?php

namespace App\Dto\Input;

use ApiPlatform\Metadata\ApiProperty;
use App\Entity\Command;
use phpDocumentor\Reflection\Types\Boolean;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

final class CommandInput
{
    #[Assert\NotBlank(message: 'validators.command.name.not_blank')]
    #[Groups(['command:write'])]
    #[ApiProperty(
        description: 'El nombre del comando',
        example: 'Command 1',
    )]
    public ?string $name = null;

    #[Assert\NotBlank(message: 'validators.command.script.not_blank')]
    #[Groups(['command:write'])]
    #[ApiProperty(
        description: 'El script del comando',
        example: 'echo "Hello World"',
    )]
    public ?string $script = null;

    #[Groups(['command:write'])]
    #[ApiProperty(
        description: 'Es interno el comando?',
        example: 'true',
    )]
    public ?bool $readOnly = false;

    #[Groups(['command:write'])]
    #[ApiProperty(
        description: 'Esta activo?',
        example: 'true',
    )]
    public ?bool $enabled = true;

    #[Groups(['command:write'])]
    #[ApiProperty(
        description: 'Tiene parámetros?',
        example: 'true',
    )]
    public ?bool $parameters = true;

    #[Groups(['command:write'])]
    #[ApiProperty(
        description: 'Los comentarios del comando',
        example: 'Comentarios del comando',
    )]
    public ?string $comments = null;

    public function __construct(?Command $command = null)
    {
        if (!$command) {
            return;
        }

        $this->name = $command->getName();
        $this->script = $command->getScript();
        $this->enabled = $command->isEnabled();
        $this->readOnly = $command->isReadOnly();
        $this->parameters = $command->isParameters();
        $this->comments = $command->getComments();
    }

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

        $command->setName($this->name);
        $command->setScript($this->script);
        $command->setEnabled($this->enabled);
        $command->setReadOnly($this->readOnly);
        $command->setParameters($this->parameters);
        $command->setComments($this->comments);

        return $command;
    }
}