<?php
declare(strict_types=1);
$contentDirection = $topic !== null ? $localeContext->contentDirection($topic['content_locale'] ?? null) : $localeContext->htmlAttributes()['dir'];
$postingRules = is_array($postingRules ?? null) ? array_replace(\App\Community\PostingPolicyService::defaults(), $postingRules) : \App\Community\PostingPolicyService::defaults();
$topicCommentMin = max(0, (int) ($postingRules['topic_comment_min'] ?? 3));
$topicCommentMax = max(0, (int) ($postingRules['topic_comment_max'] ?? 600));
$topicSlug = (string) ($topic['slug'] ?? '');
$topicUrl = quaeriva_url('d/' . rawurlencode($topicSlug));
$authorUsername = trim((string) ($topic['author_username'] ?? ''));
$isReported = in_array((int) ($topic['id'] ?? 0), (array) ($reportedItems['topic'] ?? []), true);
$reportReasons = ['spam', 'abuse', 'duplicate', 'misleading', 'other'];
?>
<section class="content-section topic-detail-section">
  <div class="container topic-container" style="max-inline-size: 55rem;">
    <?php if (trim((string) ($notice ?? '')) !== ''): ?><div class="notice" role="status"><?= quaeriva_escape($notice) ?></div><?php endif; ?>
    <?php if ((string) ($_GET['commented'] ?? '') === 'pending'): ?><div class="notice" role="status"><?= quaeriva_t($translator, 'topics.comment_pending') ?></div><?php endif; ?>

    <div class="nexus-q-header">
      <h1 dir="<?= quaeriva_escape($contentDirection) ?>" style="margin:0 0 0.5rem 0; font-size:1.625rem; font-weight:600;"><?= quaeriva_escape($topic['title'] ?? '') ?></h1>
      <div class="nexus-q-meta">
        <span>u/<?= quaeriva_escape($authorUsername ?: $translator->get('common.anonymous')) ?></span>
        <span><time datetime="<?= quaeriva_escape((string) ($topic['created_at'] ?? '')) ?>"><?= quaeriva_escape(quaeriva_relative_time($topic['created_at'] ?? '', $translator)) ?></time></span>
        <?php if (trim((string) ($topic['category_slug'] ?? '')) !== ''): ?>
          <a class="nexus-tag-chip" href="<?= quaeriva_escape(quaeriva_url('c/' . rawurlencode((string) $topic['category_slug']))) ?>"><?= quaeriva_escape($topic['category_name'] ?? '') ?></a>
        <?php endif; ?>
      </div>
    </div>

    <article style="margin-block-end:2rem;">
      <div class="rich-text" dir="<?= quaeriva_escape($contentDirection) ?>" style="font-size:0.938rem; line-height:1.65;">
        <?= quaeriva_rich_text($topic['body'] ?? '', 'full') ?>
      </div>

      <div style="display:flex; justify-content:space-between; align-items:center; margin-block-start:1.5rem; padding-block-start:0.75rem; border-block-start:1px solid var(--color-border); font-size:0.8125rem;">
        <?php if (($site['follows_enabled'] ?? true) === true && $currentUser !== null): ?>
          <form method="post" action="<?= quaeriva_escape(quaeriva_url('d/' . rawurlencode($topicSlug) . '/follow')) ?>">
            <input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken) ?>">
            <button type="submit" style="background:none; border:none; color:var(--color-muted); cursor:pointer;"><?= quaeriva_t($translator, !empty($isFollowing) ? 'topics.unfollow' : 'topics.follow') ?></button>
          </form>
        <?php endif; ?>
      </div>
    </article>

    <!-- Comments -->
    <section class="topic-comments-section">
      <h2 style="font-size:1.25rem; font-weight:600; padding-block-end:0.5rem; border-block-end:1px solid var(--color-border); margin-block-end:1rem;">
        <?= quaeriva_t($translator, 'topics.comments_heading', ['count' => count($comments ?? [])]) ?>
      </h2>

      <?php if (($comments ?? []) !== []): ?>
        <div style="display:flex; flex-direction:column; gap:0.75rem; margin-block-end:1.5rem;">
          <?php foreach ($comments as $comment):
            $commentId = (int) ($comment['id'] ?? 0);
            $cUsername = trim((string) ($comment['author_username'] ?? ''));
          ?>
            <div id="comment-<?= $commentId ?>" style="padding-block:0.5rem; border-block-end:1px solid var(--color-border); font-size:0.875rem;">
              <div class="rich-text" dir="<?= quaeriva_escape($contentDirection) ?>">
                <?= quaeriva_rich_text($comment['body'] ?? '', 'comment') ?>
              </div>
              <div style="font-size:0.75rem; color:var(--color-muted); margin-block-start:0.25rem;">
                – <strong>u/<?= quaeriva_escape($cUsername ?: $translator->get('common.anonymous')) ?></strong>
                <time datetime="<?= quaeriva_escape((string) ($comment['created_at'] ?? '')) ?>"><?= quaeriva_escape(quaeriva_relative_time($comment['created_at'] ?? '', $translator)) ?></time>
              </div>
            </div>
          <?php endforeach; ?>
        </div>
      <?php endif; ?>

      <?php if ($currentUser !== null): ?>
        <div style="margin-block-start:1.5rem;">
          <h3 style="font-size:1rem; margin:0 0 0.5rem 0;"><?= quaeriva_t($translator, 'topics.add_comment') ?></h3>
          <form method="post" action="<?= quaeriva_escape(quaeriva_url('d/' . rawurlencode($topicSlug) . '/comments')) ?>">
            <input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken) ?>">
            <?= quaeriva_bot_guard_fields() ?>
            <textarea name="body" class="comment-textarea" dir="<?= quaeriva_escape($contentDirection) ?>" rows="3"<?= quaeriva_rich_editor_attributes($translator, 'comment', '') ?><?= $topicCommentMax > 0 ? ' maxlength="' . $topicCommentMax . '"' : '' ?><?= $topicCommentMin > 0 ? ' minlength="' . $topicCommentMin . '"' : '' ?> required placeholder="<?= quaeriva_t($translator, 'topics.comment_placeholder') ?>"></textarea>
            <button class="button button-small" type="submit" style="margin-block-start:0.5rem;"><?= quaeriva_t($translator, 'topics.comment_submit') ?></button>
          </form>
        </div>
      <?php endif; ?>
    </section>
  </div>
</section>
