<?php

namespace App\Dto\Output;

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

#[Get(shortName: 'CommandGroup')]
final class CommandGroupOutput extends AbstractOutput
{
    #[Groups(['command-group:read', 'command-task:read'])]
    #[ApiProperty(
        description: 'El nombre del grupo de comandos',
        example: 'Comandos de sistema'
    )]
    public string $name;

    #[Groups(['command-group:read', 'command-task:read'])]
    #[ApiProperty(
        description: 'Los comandos incluidos en el grupo',
        example: []
    )]
    public array $commands = [];

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

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

    public function __construct(CommandGroup $commandGroup)
    {
        parent::__construct($commandGroup);

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

        $this->commands = $commandGroup->getCommands()->map(
            fn(Command $command) => new CommandOutput($command)
        )->toArray();

        $this->createdAt = $commandGroup->getCreatedAt();
        $this->createdBy = $commandGroup->getCreatedBy();
    }
}