<?php

/**
 * Frontend for categories or list of records.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at https://mozilla.org/MPL/2.0/.
 *
 * @package   phpMyFAQ
 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
 * @copyright 2002-2026 phpMyFAQ Team
 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      https://www.phpmyfaq.de
 * @since     2002-08-27
 */

use phpMyFAQ\Category;
use phpMyFAQ\Filter;
use phpMyFAQ\Helper\CategoryHelper;
use phpMyFAQ\Language\Plurals;
use phpMyFAQ\Link;
use phpMyFAQ\Translation;
use phpMyFAQ\Twig\TwigWrapper;
use Symfony\Component\HttpFoundation\Request;

if (!defined('IS_VALID_PHPMYFAQ')) {
    http_response_code(400);
    exit();
}

$request = Request::createFromGlobals();

$faqConfig = $container->get('phpmyfaq.configuration');
$user = $container->get('phpmyfaq.user.current_user');

$faqSession = $container->get('phpmyfaq.user.session');
$faqSession->setCurrentUser($user);

$selectedCategoryId = Filter::filterVar($request->query->get('cat'), FILTER_VALIDATE_INT);
$subCategoryContent = null;

if ($selectedCategoryId === 0) {
    $selectedCategoryId = null;
}

$categoryHelper = new CategoryHelper();
$categoryHelper->setPlurals(new Plurals());

$twig = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates/');
$twigTemplate = $twig->loadTemplate('./show.twig');

if (!is_null($selectedCategoryId)) {
    $faqSession->userTracking('show_category', $selectedCategoryId);

    $categoryData = $category->getCategoryData($selectedCategoryId);
    $records = $faq->renderFaqsByCategoryId(
        $selectedCategoryId,
        $faqConfig->get('records.orderby'),
        $faqConfig->get('records.sortby'),
    );

    if (empty($records) || $category->getChildNodes((int) $selectedCategoryId)) {
        $subCategory = new Category($faqConfig, $currentGroups, true);
        $subCategory->setUser($currentUser);
        $subCategory->transform($selectedCategoryId);
        $categoryHelper->setConfiguration($faqConfig)->setCategory($subCategory);

        if (empty($records)) {
            $records = sprintf(
                '<div class="mb-5 alert alert-info">%s</div>',
                Translation::get(key: 'msgErrorNoRecords'),
            );
        }

        if (
            (
                is_countable($category->getChildNodes((int) $selectedCategoryId))
                    ? count($category->getChildNodes((int) $selectedCategoryId))
                    : 0
            ) !== 0
        ) {
            $categoryFaqsHeader = Translation::get(key: 'msgSubCategories');
            $subCategoryContent = $categoryHelper->renderCategoryTree($selectedCategoryId);
            $templateVars = [
                ...$templateVars,
                'categorySubsHeader' => $categoryFaqsHeader,
            ];
        }
    }

    $up = '';
    if ($categoryData->getId() !== 0) {
        $url = sprintf('%sindex.php?action=show&cat=%d', $faqConfig->getDefaultUrl(), $categoryData->getParentId());

        $text = $category->getCategoryName($categoryData->getParentId()) === ''
            ? Translation::get(key: 'msgCategoryUp')
            : $category->getCategoryName($categoryData->getParentId());

        $link = new Link($url, $faqConfig);
        $link->setTitle($text);
        $link->text = $text;
        $link->tooltip = Translation::get(key: 'msgCategoryUp');

        $up = sprintf('<i class="bi bi-arrow-90deg-up"></i> %s', $link->toHtmlAnchor());
    }

    if (!is_null($categoryData->getImage()) && strlen((string) $categoryData->getImage()) > 0) {
        $categoryImage = $faqConfig->getDefaultUrl() . 'content/user/images/' . $categoryData->getImage();
    }

    $categoryHeader = Translation::get(key: 'msgEntriesIn') . $categoryData->getName();

    // Twig template variables
    $templateVars = [
        ...$templateVars,
        'categoryFaqsHeader' => $categoryData->getName(),
        'categoryDescription' => $categoryData->getDescription() ?? '',
        'categorySubsHeader' => Translation::get(key: 'msgSubCategories'),
        'categoryImage' => $categoryImage ?? null,
        'categoryContent' => $records,
        'subCategoryContent' => $subCategoryContent,
        'categoryLevelUp' => $up,
    ];
} else {
    $selectedCategoryId = 0;
    $faqSession->userTracking('show_all_categories', 0);

    $categoryHelper
        ->setConfiguration($faqConfig)
        ->setCategory($category);

    $categoryHeader = Translation::get(key: 'msgFullCategories');

    // Twig template variables
    $templateVars = [
        ...$templateVars,
        'categoryFaqsHeader' => Translation::get(key: 'msgShowAllCategories'),
        'categoryDescription' => Translation::get(key: 'msgCategoryDescription'),
        'categorySubsHeader' => Translation::get(key: 'msgSubCategories'),
        'categoryContent' => $categoryHelper->renderCategoryTree($selectedCategoryId),
        'subCategoryContent' => Translation::get(key: 'msgSubCategoryContent'),
        'categoryLevelUp' => '',
    ];
}

$templateVars = [
    ...$templateVars,
    'title' => sprintf('%s - %s', $categoryHeader, $faqConfig->getTitle()),
    'metaDescription' => sprintf(Translation::get(key: 'msgCategoryMetaDesc'), $faqConfig->getTitle()),
    'categoryHeader' => $categoryHeader,
    'breadcrumb' => $category->getPathWithStartpage($selectedCategoryId, '/', true),
];

return $templateVars;
