<?php

namespace App\Dto\Output;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;
use App\Entity\Client;
use App\Entity\Command;
use App\Entity\CommandGroup;
use App\Entity\CommandTask;
use Symfony\Component\Serializer\Annotation\Groups;

#[Get(shortName: 'CommandTask')]
final class CommandTaskOutput extends AbstractOutput
{
    #[Groups(['command-task:read'])]
    #[ApiProperty(
        description: 'El nombre de la tarea de comando',
        example: 'Actualización nocturna'
    )]
    public ?string $name = null;

    #[Groups(['command-task:read'])]
    #[ApiProperty(
        description: 'Los clientes asignados a la tarea',
        example: []
    )]
    public array $clients = [];

    #[Groups(['command-task:read'])]
    #[ApiProperty(
        description: 'La unidad organizativa asociada a la tarea',
        example: null
    )]
    public ?OrganizationalUnitOutput $organizationalUnit = null;

    #[Groups(['command-task:read'])]
    #[ApiProperty(
        description: 'Fecha de la última ejecución',
        example: '2024-01-15T02:00:00+00:00'
    )]
    public ?\DateTimeInterface $lastExecution = null;

    #[Groups(['command-task:read'])]
    #[ApiProperty(
        description: 'Fecha de la próxima ejecución programada',
        example: '2024-01-16T02:00:00+00:00'
    )]
    public ?\DateTimeInterface $nextExecution = null;

    #[Groups(['command-task:read'])]
    #[ApiProperty(
        description: 'Notas adicionales sobre la tarea',
        example: 'Ejecutar fuera del horario escolar'
    )]
    public ?string $notes = null;

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

    #[Groups(['command-task:read'])]
    #[ApiProperty(
        description: 'Fecha de creación de la tarea',
        example: '2024-01-15T10:30:00+00:00'
    )]
    public \DateTime $createdAt;

    #[Groups(['command-task:read'])]
    #[ApiProperty(
        description: 'Usuario que creó este registro',
        example: 'admin'
    )]
    public ?string $createdBy = null;


    public function __construct(CommandTask $commandTask)
    {
        parent::__construct($commandTask);

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

        $this->clients = $commandTask->getClients()->map(
            fn(Client $client) => new ClientOutput($client)
        )->toArray();

        if ($commandTask->getOrganizationalUnit()) {
            $this->organizationalUnit = new OrganizationalUnitOutput($commandTask->getOrganizationalUnit());
        }
        $this->notes = $commandTask->getNotes();
        $this->scope = $commandTask->getScope();
        $this->lastExecution = $commandTask->getLastExecution();
        $this->nextExecution = $commandTask->getNextExecution();
        $this->createdAt = $commandTask->getCreatedAt();
        $this->createdBy = $commandTask->getCreatedBy();
    }
}