<?php

namespace Containers\Dto;

readonly class ContainerImageListArgumentsDto
{
    public function __construct(
        public int     $page = 1,
        public int     $per_page = 20,
        public string  $sort_column = 'created_at',
        public string  $sort_direction = 'desc',
        public ?string $hostkey = null,
        public ?string $owner = null,
        public ?string $repository = null,
        public ?string $tag = null,
        public ?string $engine = null,
    ) {
    }

    public static function fromArray(array $data): self
    {
        return new self(
            page: isset($data['page']) ? max(1, (int) $data['page']) : 1,
            per_page: isset($data['per_page']) ? max(1, min(100, (int) $data['per_page'])) : 20,
            sort_column: $data['sort'] ?? 'created_at',
            sort_direction: strtolower($data['order'] ?? 'desc') === 'asc' ? 'asc' : 'desc',
            hostkey: $data['hostkey'] ?? null,
            owner: $data['owner'] ?? null,
            repository: $data['repository'] ?? null,
            tag: $data['tag'] ?? null,
            engine: $data['engine'] ?? null,
        );
    }

    public function skip(): int
    {
        return ($this->page - 1) * $this->per_page;
    }
}
