<?php

declare(strict_types=1);

namespace App\Domain\Reader\Infrastructure\ApiPlatform\State\Provider;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Domain\Reader\Application\Query\GetChapterPages;
use App\Domain\Reader\Application\QueryHandler\GetChapterPagesHandler;
use App\Domain\Reader\Application\Response\ChapterPagesResponse;

final readonly class ChapterPagesProvider implements ProviderInterface
{
    public function __construct(
        private GetChapterPagesHandler $handler
    ) {
    }

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ChapterPagesResponse
    {
        $page = $context['filters']['page'] ?? 1;
        $itemsPerPage = $context['filters']['itemsPerPage'] ?? 20;

        return $this->handler->handle(
            new GetChapterPages(
                $uriVariables['chapterId'],
                (int) $page,
                (int) $itemsPerPage
            )
        );
    }
}
