<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Sourcing\Query;

final class QueryRequest
{
    public function __construct(
        private readonly string $readModelId,
        private readonly QueryFilter $filter = new QueryFilter(),
        private readonly QuerySorter $sorter = new QuerySorter(),
        private readonly QueryPaginator $paginator = new QueryPaginator()
    ) {}

    public static function create(
        string $readModelId,
        array $criteria = [],
        string $sortField = '',
        string $sortDirection = 'asc',
        int $page = 1,
        int $pageSize = 25
    ): self {
        return new self(
            $readModelId,
            QueryFilter::create($criteria),
            QuerySorter::create($sortField, $sortDirection),
            QueryPaginator::create($page, $pageSize)
        );
    }

    public function readModelId(): string { return $this->readModelId; }
    public function filter(): QueryFilter { return $this->filter; }
    public function sorter(): QuerySorter { return $this->sorter; }
    public function paginator(): QueryPaginator { return $this->paginator; }

    public function toArray(): array
    {
        return [
            'readModelId' => $this->readModelId,
            'filter' => $this->filter->toArray(),
            'sorter' => $this->sorter->toArray(),
            'paginator' => $this->paginator->toArray(),
        ];
    }
}
