<?php

namespace App\Dto\Output;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;
use App\Entity\ImageImageRepository;
use App\Entity\Partition;
use Symfony\Component\Serializer\Annotation\Groups;

#[Get(shortName: 'Partition')]
class PartitionOutput extends AbstractOutput
{
    #[Groups(['partition:read', 'client:read'])]
    #[ApiProperty(
        description: 'El número del disco',
        example: 1
    )]
    public ?int $diskNumber = null;

    #[Groups(['partition:read', 'client:read'])]
    #[ApiProperty(
        description: 'El número de la partición',
        example: 1
    )]
    public ?int $partitionNumber = null;

    #[Groups(['partition:read', 'client:read'])]
    #[ApiProperty(
        description: 'El código del tipo de partición',
        example: '83'
    )]
    public ?string $partitionCode = null;

    #[Groups(['partition:read', 'client:read'])]
    #[ApiProperty(
        description: 'El tamaño de la partición en MB',
        example: 51200
    )]
    public ?int $size = null;

    #[Groups(['partition:read', 'client:read'])]
    #[ApiProperty(
        description: 'El contenido de la caché',
        example: 'system'
    )]
    public ?string $cacheContent = null;

    #[Groups(['partition:read', 'client:read'])]
    #[ApiProperty(
        description: 'El sistema de archivos',
        example: 'ext4'
    )]
    public ?string $filesystem = null;

    #[Groups(['partition:read', 'client:read'])]
    #[ApiProperty(
        description: 'El sistema operativo instalado en la partición',
        example: null
    )]
    public ?OperativeSystemOutput $operativeSystem = null;

    #[Groups(['partition:read', 'client:read'])]
    #[ApiProperty(
        description: 'La imagen asociada a la partición',
        example: null
    )]
    public ?ImageImageRepositoryOutput $image = null;

    #[Groups(['partition:read', 'client:read'])]
    #[ApiProperty(
        description: 'El porcentaje de uso de memoria',
        example: 45
    )]
    public ?int $memoryUsage = null;

    public function __construct(Partition $partition)
    {
        parent::__construct($partition);

        $this->diskNumber = $partition->getDiskNumber();
        $this->partitionNumber = $partition->getPartitionNumber();
        $this->partitionCode = $partition->getPartitionCode();
        $this->size = $partition->getSize() / 1024 ;
        $this->cacheContent = $partition->getCacheContent();
        $this->filesystem = $partition->getFilesystem();

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

        if ($partition->getImage()) {
            $this->image = new ImageImageRepositoryOutput($partition->getImage());
        }

        $this->memoryUsage = $partition->getMemoryUsage() / 100;
    }
}