<?php

namespace Containers\Dto;

readonly class ContainerImageDto
{
    public function __construct(
        public string  $hostkey,
        public string  $owner,
        public string  $id,
        public ?string $repository,
        public ?string $tag,
        public ?int    $size_bytes,
        public ?string $created_at,
        public ?string $collected_at,
    ) {
    }

    public static function fromRow(array $row): self
    {
        return new self(
            hostkey: (string) ($row['hostkey'] ?? ''),
            owner: (string) ($row['owner'] ?? ''),
            id: (string) ($row['id'] ?? ''),
            repository: $row['repository'] ?? null,
            tag: $row['tag'] ?? null,
            size_bytes: isset($row['size_bytes']) ? (int) $row['size_bytes'] : null,
            created_at: $row['created_at'] ?? null,
            collected_at: $row['collected_at'] ?? null,
        );
    }

    public function toArray(): array
    {
        return [
            'hostkey' => $this->hostkey,
            'owner' => $this->owner,
            'id' => $this->id,
            'repository' => $this->repository,
            'tag' => $this->tag,
            'size_bytes' => $this->size_bytes,
            'created_at' => $this->created_at,
            'collected_at' => $this->collected_at,
        ];
    }
}
