<?php
declare(strict_types=1);
$topicsList = is_array($topics ?? null) ? $topics : [];
$canCreate = is_array($currentUser ?? null);
?>
<?php $values = is_array($values ?? null) ? $values : []; ?>
<?php if ((string) ($mode ?? 'listing') === 'new'): ?>
  <section class="content-section topics-page">
    <div class="container">
      <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 ((is_array($categories ?? null) ? $categories : []) as $category): ?><option value="<?= (int) ($category['id'] ?? 0) ?>"<?= (int) ($values['category_id'] ?? 0) === (int) ($category['id'] ?? 0) ? ' selected' : '' ?>><?= str_repeat('&mdash; ', 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 ((is_array($languages ?? null) ? $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>
    </div>
  </section>
<?php return; endif; ?>
<section class="content-section topics-index-section">
  <div class="container topics-container">
    <div style="display:flex; justify-content:space-between; align-items:center; flex-wrap:wrap; gap:1rem; margin-block-end:1rem;">
      <h1 style="margin:0; font-size:1.5rem; font-weight:800;"><?= quaeriva_t($translator, 'topics.title') ?></h1>
      <?php if ($canCreate): ?>
        <a class="button button-small" href="<?= quaeriva_escape(quaeriva_url('d/new')) ?>"><?= quaeriva_t($translator, 'topics.start') ?></a>
      <?php endif; ?>
    </div>

    <?php if ($topicsList === []): ?>
      <div class="empty-state" style="background:var(--color-surface); border:1px solid var(--color-border); border-radius:var(--radius-sm);">
        <p><?= quaeriva_t($translator, 'topics.empty_hint') ?></p>
      </div>
    <?php else: ?>
      <div class="community-feed">
        <?php foreach ($topicsList as $topic):
          $topicSlug = (string) ($topic['slug'] ?? '');
          $topicUrl = quaeriva_url('d/' . rawurlencode($topicSlug));
          $authorUsername = trim((string) ($topic['author_username'] ?? ''));
          $commentsCount = (int) ($topic['comments_count'] ?? 0);
          $viewsCount = (int) ($topic['views_count'] ?? 0);
        ?>
          <article class="orbit-card">
            <div class="orbit-vote-rail">
              <span class="vote-icon">💬</span>
              <strong><?= $commentsCount ?></strong>
            </div>
            <div class="orbit-card-content">
              <div class="orbit-post-meta">
                <?php if (trim((string) ($topic['category_slug'] ?? '')) !== ''): ?>
                  <a class="orbit-sub-badge" href="<?= quaeriva_escape(quaeriva_url('c/' . rawurlencode((string) $topic['category_slug']))) ?>">c/<?= quaeriva_escape($topic['category_name'] ?? '') ?></a>
                  <span aria-hidden="true">•</span>
                <?php endif; ?>
                <span>u/<?= quaeriva_escape($authorUsername ?: $translator->get('common.anonymous')) ?></span>
                <span aria-hidden="true">•</span>
                <time datetime="<?= quaeriva_escape((string) ($topic['created_at'] ?? '')) ?>"><?= quaeriva_escape(quaeriva_relative_time($topic['created_at'] ?? '', $translator)) ?></time>
              </div>

              <h3 class="orbit-post-title"><a dir="auto" href="<?= quaeriva_escape($topicUrl) ?>"><?= quaeriva_escape((string) ($topic['title'] ?? '')) ?></a></h3>
              <p class="orbit-post-excerpt" dir="auto"><?= quaeriva_escape(mb_strimwidth(strip_tags((string) ($topic['body'] ?? '')), 0, 180, '…', 'UTF-8')) ?></p>

              <div class="orbit-action-bar">
                <a class="orbit-pill-btn" href="<?= quaeriva_escape($topicUrl) ?>">💬 <bdi><?= $commentsCount ?></bdi> <?= quaeriva_t($translator, 'topics.comments') ?></a>
                <span class="orbit-pill-btn">👁 <bdi><?= $viewsCount ?></bdi> <?= quaeriva_t($translator, 'common.views') ?></span>
              </div>
            </div>
          </article>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>
</section>
