<?php

namespace App\Dto\Input;

use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\CommandTaskOutput;
use App\Entity\CommandTaskScript;
use Symfony\Component\Serializer\Annotation\Groups;

final class CommandTaskScriptInput
{
    #[Groups(['command-task-script:write'])]
    #[ApiProperty(
        description: 'El contenido del script',
        example: '#!/bin/bash\necho "Hello World"'
    )]
    public ?string $content = null;

    #[Groups(['command-task-script:write'])]
    #[ApiProperty(
        description: 'El tipo de script',
        example: 'bash'
    )]
    public ?string $type = null;

    #[Groups(['command-task-script:write'])]
    #[ApiProperty(
        description: 'Los parámetros del script',
        example: ['param1' => 'value1', 'param2' => 'value2']
    )]
    public ?array $parameters = null;

    #[Groups(['command-task-script:write'])]
    #[ApiProperty(
        description: 'La tarea de comando asociada',
        example: null
    )]
    public ?CommandTaskOutput $commandTask = null;

    #[Groups(['command-task-script:write'])]
    #[ApiProperty(
        description: 'El orden de ejecución del script',
        example: 1
    )]
    public ?int $order = null;

    public function __construct(?CommandTaskScript $commandTaskScript = null)
    {
        if (!$commandTaskScript) {
            return;
        }

        $this->content = $commandTaskScript->getContent();
        $this->type = $commandTaskScript->getType();
        $this->order = $commandTaskScript->getExecutionOrder();
        $this->parameters = $commandTaskScript->getParameters();
        $this->commandTask = new CommandTaskOutput($commandTaskScript->getCommandTask());
    }

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

        if ($this->commandTask) {
            $commandTaskScript->setCommandTask($this->commandTask->getEntity());
        }
        $commandTaskScript->setType($this->type);
        $commandTaskScript->setParameters($this->parameters);
        $commandTaskScript->setContent($this->content);
        $commandTaskScript->setExecutionOrder($this->order);

        return $commandTaskScript;
    }
}