<?php

namespace App\Dto\Output;

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'])]
    public string $name;

    #[Groups(['command-group:read', 'command-task:read'])]
    public array $commands = [];

    #[Groups(['command-group:read'])]
    public \DateTime $createdAt;

    #[Groups(['command-group:read'])]
    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();
    }
}