<?php

namespace App\Dto\Output;

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'])]
    public ?string $name = '';
    #[Groups(['image:read', 'image-image-repository:read'])]
    public ?bool $remotePc = null;

    #[Groups(['image:read', 'image-image-repository:read'])]
    public ?bool $isGlobal = null;

    #[Groups(['image:read', 'image-image-repository:read'])]
    public ?string $type = '';

    #[Groups(['image:read'])]
    public ?SoftwareProfileOutput $softwareProfile = null;

    /**
     * @var ImageRepositoryOutput[]|null
     */
    #[Groups(['image:read'])]
    public ?array $imageRepositories = [];

    #[Groups(['image:read'])]
    public ?array $partitionInfo = null;

    #[Groups(['image:read', 'image-image-repository:read'])]
    public ?int $version = null;

    #[Groups(['image:read'])]
    public \DateTime $createdAt;

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