<?php

declare(strict_types=1);

namespace App\Domain\Reader\Application\QueryHandler;

use App\Domain\Reader\Application\Query\GetChapterContext;
use App\Domain\Reader\Application\Response\ChapterContextResponse;
use App\Domain\Reader\Domain\Contract\Repository\ChapterRepositoryInterface;
use App\Domain\Reader\Domain\ValueObject\ChapterId;

final readonly class GetChapterContextHandler
{
    public function __construct(
        private ChapterRepositoryInterface $chapterRepository
    ) {
    }

    public function handle(GetChapterContext $query): ChapterContextResponse
    {
        $chapterId = new ChapterId($query->getChapterId());

        $context = $this->chapterRepository->getChapterContext($chapterId);

        return new ChapterContextResponse(
            id: $query->getChapterId(),
            mangaId: $context->getMangaId(),
            title: $context->getChapterTitle(),
            number: $context->getNumber(),
            totalPages: $context->getTotalPages(),
            previousChapterId: $context->getPreviousChapterId()?->getValue(),
            nextChapterId: $context->getNextChapterId()?->getValue(),
        );
    }
}
