<?php

namespace App\Dto\Output;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;
use App\Entity\Image;
use App\Entity\ImageImageRepository;
use App\Entity\ImageRepository;
use App\Entity\Software;
use Symfony\Component\Serializer\Annotation\Groups;

#[Get(shortName: 'Image')]
final class ImageOutput extends AbstractOutput
{
    #[Groups(['image:read', 'image-image-repository:read', 'git-image-repository:read'])]
    #[ApiProperty(
        description: 'El nombre de la imagen',
        example: 'Windows 10 Pro'
    )]
    public ?string $name = '';

    #[Groups(['image:read', 'image-image-repository:read'])]
    #[ApiProperty(
        description: 'Indica si es una imagen de PC remoto',
        example: false
    )]
    public ?bool $remotePc = null;

    #[Groups(['image:read', 'image-image-repository:read'])]
    #[ApiProperty(
        description: 'Indica si la imagen es global',
        example: true
    )]
    public ?bool $isGlobal = null;

    #[Groups(['image:read', 'image-image-repository:read'])]
    #[ApiProperty(
        description: 'El tipo de imagen',
        example: 'basic'
    )]
    public ?string $type = '';

    #[Groups(['image:read'])]
    #[ApiProperty(
        description: 'El perfil de software asociado a la imagen',
        example: null
    )]
    public ?SoftwareProfileOutput $softwareProfile = null;

    /**
     * @var ImageRepositoryOutput[]|null
     */
    #[Groups(['image:read'])]
    #[ApiProperty(
        description: 'Los repositorios donde está almacenada la imagen',
        example: []
    )]
    public ?array $imageRepositories = [];

    #[Groups(['image:read'])]
    #[ApiProperty(
        description: 'Información sobre las particiones de la imagen',
        example: []
    )]
    public ?array $partitionInfo = null;

    #[Groups(['image:read', 'image-image-repository:read'])]
    #[ApiProperty(
        description: 'La versión de la imagen',
        example: 1
    )]
    public ?int $version = null;

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

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

    public function __construct(Image $image)
    {
        parent::__construct($image);

        $this->name = $image->getName();
        $this->softwareProfile = $image->getSoftwareProfile() ? new SoftwareProfileOutput($image->getSoftwareProfile()) : null;

        $this->imageRepositories = $image->getImageImageRepositories()->map(
            fn(ImageImageRepository $image) => new ImageImageRepositoryOutput($image)
        )->toArray();

        $this->version = $image->getVersion();
        $this->type = $image->getType();
        $this->partitionInfo = json_decode($image->getPartitionInfo(), true);
        $this->remotePc = $image->isRemotePc();
        $this->isGlobal = $image->isGlobal();
        $this->createdAt = $image->getCreatedAt();
        $this->createdBy = $image->getCreatedBy();
    }
}