<?php

declare(strict_types=1);

namespace Johncms\Modules\Forum\Application\Controllers;

use Johncms\Http\Controller\ControllerContext;
use Johncms\Modules\Forum\Application\Services\ForumSectionPathService;
use Johncms\Modules\Forum\Application\Services\ForumTopicPathService;

final readonly class ForumPathController
{
    public function __construct(
        private ControllerContext $controllerContext,
        private ForumSectionPathService $sectionPathService,
        private ForumTopicPathService $topicPathService,
        private ForumSectionController $forumSectionController,
        private ForumTopicController $forumTopicController,
    ) {
        $this->controllerContext->initModule('forum');
    }

    public function __invoke(string $sectionPath): string
    {
        if ($this->sectionPathService->findSectionByPath($sectionPath) !== null) {
            return $this->forumSectionController->__invoke($sectionPath);
        }

        if ($this->topicPathService->parseTopicPath('/forum/' . ltrim($sectionPath, '/')) !== null) {
            return $this->forumTopicController->__invoke($sectionPath);
        }

        pageNotFound();
    }
}
