<?php
declare(strict_types=1);
$mode = (string) ($mode ?? 'listing');
$topics = is_array($topics ?? null) ? $topics : [];
$topicsMeta = is_array($topicsMeta ?? null) ? $topicsMeta : [];
$categories = is_array($categories ?? null) ? $categories : [];
$languages = is_array($languages ?? null) ? $languages : [];
$filters = is_array($filters ?? null) ? $filters : [];
$values = is_array($values ?? null) ? $values : [];
$postingRules = is_array($postingRules ?? null) ? $postingRules : \App\Community\PostingPolicyService::defaults();
?>
<section class="content-section topics-page">
  <div class="container">
    <?php if ($mode === 'new'): ?>
      <div class="section-heading"><div><p class="eyebrow"><?= quaeriva_t($translator, 'topics.eyebrow') ?></p><h1><?= quaeriva_t($translator, 'topics.start') ?></h1><p class="section-lede"><?= quaeriva_t($translator, 'topics.start_intro') ?></p></div></div>
      <?php if (trim((string) ($error ?? '')) !== ''): ?><div class="notice notice-error" role="alert"><?= quaeriva_escape($error) ?></div><?php endif; ?>
      <form class="question-form" method="post" action="<?= quaeriva_escape(quaeriva_url('d/new')) ?>">
        <input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken ?? '') ?>">
        <?= quaeriva_bot_guard_fields() ?>
        <p class="form-hint"><?= quaeriva_t($translator, 'form.required_hint') ?></p>
        <div class="field-group">
          <label for="topic-title"><?= quaeriva_t($translator, 'topics.title_label') ?> <span aria-hidden="true">*</span></label>
          <input id="topic-title" name="title" type="text" dir="auto" value="<?= quaeriva_escape($values['title'] ?? '') ?>" maxlength="255" required>
        </div>
        <div class="field-group">
          <label for="topic-body"><?= quaeriva_t($translator, 'topics.body_label') ?> <span aria-hidden="true">*</span></label>
          <textarea id="topic-body" name="body" dir="auto" rows="10" required <?= quaeriva_rich_editor_attributes($translator, 'full', (string) ($values['body'] ?? '')) ?>><?= quaeriva_escape($values['body'] ?? '') ?></textarea>
        </div>
        <div class="field-group">
          <label for="topic-category"><?= quaeriva_t($translator, 'topics.category_label') ?></label>
          <select id="topic-category" name="category_id"><option value="0"><?= quaeriva_t($translator, 'topics.all_categories') ?></option><?php foreach ($categories as $category): ?><option value="<?= (int) ($category['id'] ?? 0) ?>"<?= (int) ($values['category_id'] ?? 0) === (int) ($category['id'] ?? 0) ? ' selected' : '' ?>><?= str_repeat('— ', min(8, max(0, (int) ($category['depth'] ?? 0)))) . quaeriva_escape((string) ($category['name'] ?? '')) ?></option><?php endforeach; ?></select>
        </div>
        <div class="field-group">
          <label for="topic-locale"><?= quaeriva_t($translator, 'topics.locale_label') ?></label>
          <select id="topic-locale" name="content_locale"><?php foreach ($languages as $language): $code = (string) ($language['code'] ?? ''); if ($code === '') continue; ?><option value="<?= quaeriva_escape($code) ?>"<?= (string) ($values['content_locale'] ?? $translator->locale()) === $code ? ' selected' : '' ?>><?= quaeriva_escape((string) ($language['native_name'] ?? $language['name'] ?? $code)) ?></option><?php endforeach; ?></select>
        </div>
        <div class="field-group">
          <label for="topic-kind"><?= quaeriva_t($translator, 'topics.kind_label') ?></label>
          <select id="topic-kind" name="kind"><option value="discussion"<?= ($values['kind'] ?? 'discussion') === 'discussion' ? ' selected' : '' ?>><?= quaeriva_t($translator, 'topics.discussion') ?></option><?php if (quaeriva_user_has_role($currentUser ?? null, 'admin', 'moderator')): ?><option value="announcement"<?= ($values['kind'] ?? '') === 'announcement' ? ' selected' : '' ?>><?= quaeriva_t($translator, 'topics.announcement') ?></option><?php endif; ?></select>
        </div>
        <button class="button" type="submit"><?= quaeriva_t($translator, 'topics.create') ?></button>
      </form>
    <?php else: ?>
      <?php if (max(0, (int) ($_GET['submitted'] ?? 0)) > 0): ?><div class="notice" role="status"><?= quaeriva_t($translator, 'topics.submission_received') ?></div><?php endif; ?>
      <?php $topicTotal = max(0, (int) ($topicsMeta['total'] ?? count($topics))); ?>
      <div class="topics-hero">
        <div class="topics-hero-copy"><p class="eyebrow"><?= quaeriva_t($translator, 'topics.eyebrow') ?></p><h1><?= quaeriva_t($translator, 'topics.title') ?></h1><p class="section-lede"><?= quaeriva_t($translator, 'topics.intro') ?></p></div>
        <div class="topics-hero-action"><span class="topics-hero-count"><?= $topicTotal ?></span><span><?= quaeriva_t($translator, 'topics.latest') ?></span><a class="button" href="<?= quaeriva_escape(quaeriva_url('d/new')) ?>"><?= quaeriva_t($translator, 'topics.start') ?></a></div>
      </div>
      <nav class="topics-tabs" aria-label="<?= quaeriva_t($translator, 'topics.nav') ?>"><a class="topics-tab" href="<?= quaeriva_escape(quaeriva_url()) ?>#latest-questions"><?= quaeriva_t($translator, 'topics.filter_all') ?></a><a class="topics-tab" href="<?= quaeriva_escape(quaeriva_url() . '#latest-questions') ?>"><?= quaeriva_t($translator, 'topics.filter_questions') ?></a><a class="topics-tab is-active" aria-current="page" href="<?= quaeriva_escape(quaeriva_url('d')) ?>"><?= quaeriva_t($translator, 'topics.filter_topics') ?></a></nav>
      <form class="topics-filter-panel" method="get" action="<?= quaeriva_escape(quaeriva_url('d')) ?>">
        <div class="topics-filter-search"><label for="topics-search"><?= quaeriva_t($translator, 'topics.search') ?></label><input id="topics-search" type="search" name="search" value="<?= quaeriva_escape($filters['search'] ?? '') ?>" placeholder="<?= quaeriva_t($translator, 'topics.search_placeholder') ?>"></div>
        <div class="topics-filter-grid">
          <label for="topics-category"><?= quaeriva_t($translator, 'topics.filter_category') ?><select id="topics-category" name="category"><option value=""><?= quaeriva_t($translator, 'topics.all_categories') ?></option><?php foreach ($categories as $category): ?><option value="<?= quaeriva_escape((string) ($category['slug'] ?? $category['id'] ?? '')) ?>"<?= ((string) ($filters['category'] ?? '') !== '' && (string) ($filters['category'] ?? '') === (string) ($category['slug'] ?? $category['id'] ?? '')) || ((int) ($filters['category_id'] ?? 0) > 0 && (int) ($filters['category_id'] ?? 0) === (int) ($category['id'] ?? 0)) ? ' selected' : '' ?>><?= str_repeat('— ', min(8, max(0, (int) ($category['depth'] ?? 0)))) . quaeriva_escape((string) ($category['name'] ?? '')) ?></option><?php endforeach; ?></select></label>
          <label for="topics-locale"><?= quaeriva_t($translator, 'topics.filter_locale') ?><select id="topics-locale" name="locale"><option value=""><?= quaeriva_t($translator, 'topics.all_locales') ?></option><?php foreach ($languages as $language): $code = (string) ($language['code'] ?? ''); if ($code === '') continue; ?><option value="<?= quaeriva_escape($code) ?>"<?= (string) ($filters['locale'] ?? '') === $code ? ' selected' : '' ?>><?= quaeriva_escape((string) ($language['native_name'] ?? $language['name'] ?? $code)) ?></option><?php endforeach; ?></select></label>
          <label for="topics-kind"><?= quaeriva_t($translator, 'topics.filter_kind') ?><select id="topics-kind" name="kind"><option value=""><?= quaeriva_t($translator, 'topics.all_kinds') ?></option><option value="discussion"<?= ($filters['kind'] ?? '') === 'discussion' ? ' selected' : '' ?>><?= quaeriva_t($translator, 'topics.discussion') ?></option><option value="announcement"<?= ($filters['kind'] ?? '') === 'announcement' ? ' selected' : '' ?>><?= quaeriva_t($translator, 'topics.announcement') ?></option></select></label>
        </div>
        <button class="button button-secondary topics-filter-submit" type="submit"><?= quaeriva_t($translator, 'common.search_submit') ?></button>
      </form>
      <div class="topics-results-head"><div><p class="eyebrow"><?= quaeriva_t($translator, 'topics.latest') ?></p><h2><?= quaeriva_t($translator, 'topics.title') ?></h2></div><span class="topics-results-count"><?= $topicTotal ?></span></div>
      <?php if ($topics === []): ?><div class="empty-state"><h2><?= quaeriva_t($translator, 'topics.no_topics') ?></h2><p><?= quaeriva_t($translator, 'topics.no_topics_hint') ?></p><a class="button button-secondary" href="<?= quaeriva_escape(quaeriva_url('d/new')) ?>"><?= quaeriva_t($translator, 'topics.start') ?></a></div><?php else: ?>
        <div class="topic-list">
          <?php foreach ($topics 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) ($topicsMeta['page'] ?? 1)); $pages = max(1, (int) ($topicsMeta['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('d', array_merge($filters, ['page' => $page - 1]))) ?>"><?= 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('d', array_merge($filters, ['page' => $page + 1]))) ?>"><?= quaeriva_t($translator, 'topics.next_page') ?></a><?php endif; ?></nav><?php endif; ?>
      <?php endif; ?>
    <?php endif; ?>
  </div>
</section>
