<?php
declare(strict_types=1);
$questions = is_array($questions ?? null) ? $questions : [];
$topics = is_array($topics ?? null) ? $topics : ['items' => [], 'total' => 0, 'page' => 1, 'per_page' => 20, 'total_pages' => 1];
$topicItems = is_array($topics['items'] ?? null) ? $topics['items'] : [];
$taxonomy = is_array($taxonomy ?? null) ? $taxonomy : [];
$type = (string) ($type ?? 'category');
$isFollowing = (bool) ($isFollowing ?? false);
$questionCount = count($questions);
$topicCount = max(0, (int) ($topics['total'] ?? count($topicItems)));
$taxonomyPath = ($type === 'tag' ? 't/' : 'c/') . rawurlencode((string) ($taxonomy['slug'] ?? ''));
$rawTab = (string) ($_GET['tab'] ?? 'all');
$activeTab = in_array($rawTab, ['all', 'questions', 'discussions'], true) ? $rawTab : 'all';
?>
<section class="content-section taxonomy-page">
  <div class="container">
    <div class="section-heading taxonomy-heading">
      <div>
        <p class="eyebrow"><?= quaeriva_t($translator, $type === 'tag' ? 'taxonomy.tag' : 'taxonomy.category') ?></p>
        <h1 dir="auto"><?= quaeriva_escape($taxonomy['name'] ?? '') ?></h1>
        <?php if (trim((string) ($taxonomy['description'] ?? '')) !== ''): ?><p class="section-lede" dir="auto"><?= quaeriva_escape($taxonomy['description']) ?></p><?php endif; ?>
      </div>
      <?php if (($site['follows_enabled'] ?? true) === true): ?>
        <div class="taxonomy-follow">
        <?php if (($currentUser ?? null) !== null): ?>
          <form method="post" action="<?= quaeriva_escape(quaeriva_url(($type === 'tag' ? 't/' : 'c/') . rawurlencode((string) ($taxonomy['slug'] ?? '')) . '/follow')) ?>">
            <input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken ?? '') ?>">
            <button class="button button-secondary" type="submit"><?= quaeriva_t($translator, $isFollowing ? 'taxonomy.unfollow' : 'taxonomy.follow') ?></button>
          </form>
        <?php else: ?>
          <a class="button button-secondary" href="<?= quaeriva_escape(quaeriva_url('login', ['redirect' => $currentPath ?? quaeriva_url()])) ?>"><?= quaeriva_t($translator, 'taxonomy.login_to_follow') ?></a>
        <?php endif; ?>
        </div>
      <?php endif; ?>
    </div>

    <!-- Category Tabs Navigation -->
    <?php if ($type === 'category' && ($questionCount > 0 || $topicCount > 0)): ?>
      <nav class="community-feed-tabs" aria-label="<?= quaeriva_t($translator, 'taxonomy.category') ?>" style="margin-block-end: 1.5rem; display: flex; gap: 0.5rem;">
        <a class="button button-small <?= $activeTab === 'all' ? '' : 'button-secondary' ?>" href="<?= quaeriva_escape(quaeriva_url($taxonomyPath)) ?>"<?= $activeTab === 'all' ? ' aria-current="page"' : '' ?>><?= quaeriva_t($translator, 'topics.filter_all') ?> (<bdi><?= $questionCount + $topicCount ?></bdi>)</a>
        <a class="button button-small <?= $activeTab === 'questions' ? '' : 'button-secondary' ?>" href="<?= quaeriva_escape(quaeriva_url($taxonomyPath, ['tab' => 'questions'])) ?>"<?= $activeTab === 'questions' ? ' aria-current="page"' : '' ?>><?= quaeriva_t($translator, 'topics.filter_questions') ?> (<bdi><?= $questionCount ?></bdi>)</a>
        <a class="button button-small <?= $activeTab === 'discussions' ? '' : 'button-secondary' ?>" href="<?= quaeriva_escape(quaeriva_url($taxonomyPath, ['tab' => 'discussions'])) ?>"<?= $activeTab === 'discussions' ? ' aria-current="page"' : '' ?>><?= quaeriva_t($translator, 'topics.filter_topics') ?> (<bdi><?= $topicCount ?></bdi>)</a>
      </nav>
    <?php endif; ?>

    <?php if ($questions === [] && $topicItems === []): ?>
      <div class="empty-state"><h2><?= quaeriva_t($translator, 'taxonomy.empty') ?></h2><p><?= quaeriva_t($translator, 'taxonomy.empty_hint') ?></p></div>
    <?php else: ?>
      <?php if (($activeTab === 'all' || $activeTab === 'questions') && $questions !== []): ?>
        <div class="section-heading"><h2><?= quaeriva_t($translator, 'taxonomy.questions_heading', ['count' => $questionCount]) ?></h2></div>
        <div class="question-list">
        <?php foreach ($questions as $question): ?>
          <article class="question-card">
            <div class="question-stats"><span><strong><?= (int) ($question['votes_count'] ?? 0) ?></strong><?= quaeriva_t($translator, 'common.votes') ?></span><span><strong><?= (int) ($question['answers_count'] ?? 0) ?></strong><?= quaeriva_t($translator, 'common.answers') ?></span></div>
            <div class="question-card-body"><h2><a dir="auto" href="<?= quaeriva_escape(quaeriva_question_url((string) ($question['slug'] ?? ''), (int) ($question['id'] ?? 0))) ?>"><?= quaeriva_escape($question['title'] ?? '') ?></a></h2><p dir="auto"><?= quaeriva_plain_text($question['body'] ?? '') ?></p><div class="question-meta"><?php if (($question['category_slug'] ?? '') !== ''): ?><a class="text-link" href="<?= quaeriva_escape(quaeriva_url('c/' . rawurlencode((string) $question['category_slug']))) ?>"><?= quaeriva_escape($question['category_name'] ?? '') ?></a><?php endif; ?><?php if (($question['author_username'] ?? '') !== ''): ?><a class="text-link" href="<?= quaeriva_escape(quaeriva_url('u/' . rawurlencode((string) $question['author_username']))) ?>"><?= quaeriva_escape($question['author_name'] ?? '') ?></a><?php else: ?><span><?= quaeriva_escape(($question['author_name'] ?? '') !== '' ? $question['author_name'] : $translator->get('common.anonymous')) ?></span><?php endif; ?></div></div>
          </article>
        <?php endforeach; ?>
        </div>
      <?php endif; ?>

      <?php if (($activeTab === 'all' || $activeTab === 'discussions') && $topicItems !== []): ?>
        <div class="section-heading" style="margin-block-start: <?= $activeTab === 'all' && $questions !== [] ? '2rem' : '0' ?>;"><h2><?= quaeriva_t($translator, 'taxonomy.discussions_heading', ['count' => $topicCount]) ?></h2></div>
        <div class="topic-list">
          <?php foreach ($topicItems as $topic): $topicSlug = trim((string) ($topic['slug'] ?? '')); if ($topicSlug === '') continue; $excerpt = trim(strip_tags(quaeriva_rich_text((string) ($topic['body'] ?? ''), 'comment'))); $excerpt = function_exists('mb_substr') ? mb_substr($excerpt, 0, 220, 'UTF-8') : substr($excerpt, 0, 220); ?>
            <article class="topic-card">
              <div class="topic-card-main"><div class="topic-card-topline"><div class="topic-badges"><?php if ((int) ($topic['is_pinned'] ?? 0) === 1): ?><span class="topic-badge topic-badge-pinned"><?= quaeriva_t($translator, 'topics.pinned') ?></span><?php endif; ?><?php if (($topic['kind'] ?? '') === 'announcement'): ?><span class="topic-badge topic-badge-announcement"><?= quaeriva_t($translator, 'topics.announcement_badge') ?></span><?php endif; ?><?php if ((int) ($topic['is_locked'] ?? 0) === 1): ?><span class="topic-badge topic-badge-muted"><?= quaeriva_t($translator, 'topics.locked') ?></span><?php endif; ?></div><?php if (trim((string) ($topic['category_name'] ?? '')) !== ''): ?><span class="topic-category"><?= quaeriva_escape((string) $topic['category_name']) ?></span><?php endif; ?></div><h3><a dir="auto" href="<?= quaeriva_escape(quaeriva_topic_url($topicSlug)) ?>"><?= quaeriva_escape($topic['title'] ?? '') ?></a></h3><p class="topic-excerpt" dir="auto"><?= quaeriva_escape($excerpt) ?></p><div class="topic-card-meta"><bdi><?= quaeriva_escape(trim((string) ($topic['author_name'] ?? '')) ?: $translator->get('common.anonymous')) ?></bdi><span aria-hidden="true">·</span><span><?= quaeriva_escape(quaeriva_relative_time($topic['last_activity_at'] ?? $topic['updated_at'] ?? null, $translator)) ?></span></div></div>
              <div class="topic-card-stats"><span><strong><?= (int) ($topic['comments_count'] ?? 0) ?></strong><small><?= quaeriva_t($translator, 'topics.comments') ?></small></span><span><strong><?= (int) ($topic['views_count'] ?? 0) ?></strong><small><?= quaeriva_t($translator, 'common.views') ?></small></span></div>
            </article>
          <?php endforeach; ?>
        </div>
        <?php $page = max(1, (int) ($topics['page'] ?? 1)); $pages = max(1, (int) ($topics['total_pages'] ?? 1)); if ($pages > 1): ?><nav class="pagination" aria-label="<?= quaeriva_t($translator, 'topics.pagination') ?>"><span><?= quaeriva_t($translator, 'topics.page_summary', ['page' => $page, 'pages' => $pages]) ?></span><?php if ($page > 1): ?><a class="button button-small button-secondary" href="<?= quaeriva_escape(quaeriva_url($taxonomyPath, ['page' => $page - 1, 'tab' => $activeTab])) ?>"><?= quaeriva_t($translator, 'topics.previous_page') ?></a><?php endif; ?><?php if ($page < $pages): ?><a class="button button-small button-secondary" href="<?= quaeriva_escape(quaeriva_url($taxonomyPath, ['page' => $page + 1, 'tab' => $activeTab])) ?>"><?= quaeriva_t($translator, 'topics.next_page') ?></a><?php endif; ?></nav><?php endif; ?>
      <?php endif; ?>
    <?php endif; ?>
  </div>
</section>
