<?php

namespace App\Dto\Output;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;
use App\Entity\Software;
use App\Entity\SoftwareProfile;
use Symfony\Component\Serializer\Annotation\Groups;

#[Get(shortName: 'SoftwareProfile')]
final class SoftwareProfileOutput extends AbstractOutput
{
    #[Groups(['software-profile:read', 'image:read'])]
    #[ApiProperty(
        description: 'La descripción del perfil de software',
        example: 'Perfil estándar para aula de informática'
    )]
    public ?string $description = '';

    #[Groups(['software-profile:read'])]
    #[ApiProperty(
        description: 'Comentarios adicionales sobre el perfil',
        example: 'Incluye software educativo básico'
    )]
    public ?string $comments = '';

    #[Groups(['software-profile:read'])]
    #[ApiProperty(
        description: 'La unidad organizativa asociada',
        example: null
    )]
    public ?OrganizationalUnitOutput $organizationalUnit = null;

    #[Groups(['software-profile:read'])]
    #[ApiProperty(
        description: 'El sistema operativo base del perfil',
        example: null
    )]
    public ?OperativeSystemOutput $operativeSystem = null;

    #[Groups(['software-profile:item:get'])]
    #[ApiProperty(
        description: 'La colección de software incluido en el perfil',
        example: []
    )]
    public ?array $softwareCollection = [];

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

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

    public function __construct(SoftwareProfile $softwareProfile)
    {
        parent::__construct($softwareProfile);

        $this->description = $softwareProfile->getDescription();
        $this->comments = $softwareProfile->getComments();

        if($softwareProfile->getOrganizationalUnit()) {
            $this->organizationalUnit = new OrganizationalUnitOutput($softwareProfile->getOrganizationalUnit());
        }

        if ($softwareProfile->getOperativeSystem()) {
            $this->operativeSystem = new OperativeSystemOutput($softwareProfile->getOperativeSystem());
        }

        $this->softwareCollection = $softwareProfile->getSoftwareCollection()->map(
            fn(Software $hardware) => new SoftwareOutput($hardware)
        )->toArray();

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