<?php

namespace Datalumo\PhpSdk\DataObjects;

class PaginatedResponse
{
    public function __construct(
        public readonly array $data,
        public readonly int $currentPage,
        public readonly int $lastPage,
        public readonly int $perPage,
        public readonly int $total,
    ) {}

    public function hasMorePages(): bool
    {
        return $this->currentPage < $this->lastPage;
    }

    public static function fromArray(array $response, callable $mapper): self
    {
        return new self(
            data: array_map($mapper, $response['data']),
            currentPage: $response['meta']['current_page'] ?? 1,
            lastPage: $response['meta']['last_page'] ?? 1,
            perPage: $response['meta']['per_page'] ?? 15,
            total: $response['meta']['total'] ?? count($response['data']),
        );
    }
}
