<?php

namespace App\Dto\Input;

use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\ClientOutput;
use App\Dto\Output\CommandGroupOutput;
use App\Dto\Output\CommandOutput;
use App\Dto\Output\OrganizationalUnitOutput;
use App\Entity\CommandTask;
use App\Model\CommandTaskStatus;
use phpDocumentor\Reflection\Types\Boolean;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

final class CommandTaskInput
{
    #[Groups(['command-task:write'])]
    #[ApiProperty(
        description: 'El nombre de la tarea',
        example: 'Tarea 1',
    )]
    public ?string $name = null;

    /**
     * @var ClientOutput[]
     */
    #[Groups(['command-task:write'])]
    #[ApiProperty(
        description: 'Los clientes de la tarea',
        example: 'Clientes de la tarea',
    )]
    public array $clients = [];

    #[Groups(['command-task:write'])]
    #[ApiProperty(
        description: 'Los comentarios de la tarea',
        example: 'Ambito de la tarea',
    )]
    public ?string $notes = null;

    #[Groups(['command-task:write'])]
    #[ApiProperty(
        description: 'El ambito de la tarea',
        example: 'Comentarios de la tarea',
    )]
    public ?string $scope = null;

    #[Groups(['command-task:write'])]
    public ?OrganizationalUnitOutput $organizationalUnit = null;

    #[Groups(['command-task:write'])]
    #[ApiProperty(
        description: 'Los parámetros de la tarea',
        example: 'Parámetros de la tarea',
    )]
    public ?array $content = null;

    public function __construct(?CommandTask $commandTask = null)
    {
        if (!$commandTask) {
            return;
        }

        $this->name = $commandTask->getName();

        if ($commandTask->getClients()) {
            foreach ($commandTask->getClients() as $client) {
                $this->clients[] = new ClientOutput($client);
            }
        }
        if ($commandTask->getOrganizationalUnit()) {
            $this->organizationalUnit = new OrganizationalUnitOutput($commandTask->getOrganizationalUnit());
        }
        $this->notes = $commandTask->getNotes();
        $this->scope = $commandTask->getScope();
        $this->content = $commandTask->getParameters();
    }

    /**
     * @throws \Exception
     */
    public function createOrUpdateEntity(?CommandTask $commandTask = null): CommandTask
    {
        if (!$commandTask) {
            $commandTask = new CommandTask();
        }

        $commandTask->setName($this->name);

        $clientsToAdd = [];
        foreach ($this->clients as $client) {
            $clientsToAdd[] = $client->getEntity();
        } 

        if ($this->organizationalUnit) {
            $commandTask->setOrganizationalUnit($this->organizationalUnit->getEntity());
        }
        $commandTask->setClients( $clientsToAdd );
        $commandTask->setNotes($this->notes);
        $commandTask->setParameters($this->content);
        $commandTask->setScope($this->scope);
        $commandTask->setNextExecution($commandTask->calculateNextExecutionDate());

        return $commandTask;
    }
}
