<?php

namespace NoahVet\Reef\Infrastructure\Http\Get;

use NoahVet\Reef\Application\Port\GetAllQueryServiceInterface as SelfInterface;
use NoahVet\Reef\Application\Query\GetAllQueryInterface;
use NoahVet\Reef\Paginator\HttpPaginatorInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class GetAllQueryService implements SelfInterface
{
    public function __construct(
        protected readonly HttpPaginatorInterface $httpPaginator,
        protected readonly RequestStack $requestStack,
    ) {
    }

    #[\Override]
    public function __invoke(GetAllQueryInterface $query): array
    {
        $request = $this->requestStack->getCurrentRequest();
        \assert(null !== $request);
        $objectFQCN = $query->objectFQCN();

        return \array_values(
            $this->httpPaginator->getResult(
                $request,
                $objectFQCN,
                [
                    'filters' => $query->filters(),
                    'keywords' => '',
                    'permissions' => $query->permissions(),
                    'sorting' => $query->sorting(),
                ],
            ),
        );
    }
}
