<?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" style="padding-block: 2rem 4rem;">
  <div class="container" style="max-inline-size: 52rem;">
    <div style="display:flex; justify-content:space-between; align-items:center; flex-wrap:wrap; gap:1rem; margin-block-end:2rem; border-block-end:1px solid #EAE3D5; padding-block-end:1rem;">
      <div>
        <p class="eyebrow" style="color:#842339; font-weight:700;"><?= quaeriva_t($translator, 'topics.eyebrow') ?></p>
        <h1 style="margin:0; font-size:1.875rem; font-weight:800; color:#1F1D1A;"><?= quaeriva_t($translator, 'topics.title') ?></h1>
      </div>
      <?php if ($canCreate): ?>
        <a class="button" href="<?= quaeriva_escape(quaeriva_url('d/new')) ?>"><?= quaeriva_t($translator, 'topics.start') ?></a>
      <?php endif; ?>
    </div>

    <?php if ($topicsList === []): ?>
      <div class="empty-state"><p><?= quaeriva_t($translator, 'topics.no_topics_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'] ?? ''));
          $bodyText = strip_tags((string) ($topic['body'] ?? ''));
          $wordCount = max(1, count(preg_split('/\s+/u', $bodyText, -1, PREG_SPLIT_NO_EMPTY) ?: []));
          $readingMinutes = max(1, (int) ceil($wordCount / 180));
          $commentsCount = (int) ($topic['comments_count'] ?? 0);
        ?>
          <article class="hikaya-story-card">
            <h2 class="hikaya-story-title"><a dir="auto" href="<?= quaeriva_escape($topicUrl) ?>"><?= quaeriva_escape((string) ($topic['title'] ?? '')) ?></a></h2>
            <p class="hikaya-story-excerpt" dir="auto"><?= quaeriva_escape(mb_strimwidth($bodyText, 0, 220, '…', 'UTF-8')) ?></p>
            <div class="hikaya-story-meta">
              <span class="hikaya-reading-time">⏱ <?= $readingMinutes ?> <?= quaeriva_t($translator, 'common.minutes') ?></span>
              <span><?= quaeriva_escape($authorUsername ?: $translator->get('common.anonymous')) ?></span>
              <span>·</span>
              <time datetime="<?= quaeriva_escape((string) ($topic['created_at'] ?? '')) ?>"><?= quaeriva_escape(quaeriva_relative_time($topic['created_at'] ?? '', $translator)) ?></time>
              <span>·</span>
              <span><?= $commentsCount ?> <?= quaeriva_t($translator, 'topics.comments') ?></span>
            </div>
          </article>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>
</section>
