<?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\GetChapterContext;
use App\Domain\Reader\Application\QueryHandler\GetChapterContextHandler;
use App\Domain\Reader\Application\Response\ChapterContextResponse;

final readonly class ChapterContextProvider implements ProviderInterface
{
    public function __construct(
        private GetChapterContextHandler $handler
    ) {
    }

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ChapterContextResponse
    {
        $response = $this->handler->handle(
            new GetChapterContext(
                $uriVariables['chapterId']
            )
        );

        return new ChapterContextResponse(
            id: $response->getId(),
            mangaId: $response->getMangaId(),
            title: $response->getTitle(),
            number: $response->getNumber(),
            totalPages: $response->getTotalPages(),
            previousChapterId: $response->getPreviousChapterId(),
            nextChapterId: $response->getNextChapterId()
        );
    }
}
