<?php
declare(strict_types=1);
$topic = is_array($topic ?? null) ? $topic : null;
$comments = is_array($comments ?? null) ? $comments : ['comments' => [], 'page' => 1, 'total_pages' => 1, 'total' => 0];
$reportedItems = is_array($reportedItems ?? null) ? $reportedItems : ['topic' => false, 'comment' => []];
$postingRules = is_array($postingRules ?? null) ? array_replace(\App\Community\PostingPolicyService::defaults(), $postingRules) : \App\Community\PostingPolicyService::defaults();
$contentDirection = $topic !== null ? $localeContext->contentDirection($topic['content_locale'] ?? null) : $localeContext->htmlAttributes()['dir'];
$slug = trim((string) ($slug ?? ($topic['slug'] ?? '')));
$viewUrl = quaeriva_topic_url($slug);
$commentMin = max(0, (int) ($postingRules['comment_min'] ?? 3));
$commentMax = max(0, (int) ($postingRules['comment_max'] ?? 600));
$reportReasons = ['spam', 'abuse', 'duplicate', 'misleading', 'other'];
?>
<section class="content-section topic-section topic-shell"><div class="container">
<?php if ($topic === null): ?><div class="empty-state"><h1><?= quaeriva_t($translator, 'topics.not_found') ?></h1><p><?= quaeriva_t($translator, 'topics.no_topics_hint') ?></p></div><?php else: ?>
  <?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; ?>
  <?php $authorName = trim((string) ($topic['author_name'] ?? '')); $username = trim((string) ($topic['author_username'] ?? '')); $avatar = trim((string) ($topic['author_avatar_url'] ?? '')); if ($avatar === '' && $authorName !== '') $avatar = quaeriva_default_avatar_url((int) ($topic['user_id'] ?? 0)); ?>
  <article class="question-detail" itemscope itemtype="https://schema.org/DiscussionForumPosting">
    <div class="question-meta question-meta-large topic-status-row"><?php if (($topic['is_pinned'] ?? 0) == 1): ?><span class="admin-badge"><?= quaeriva_t($translator, 'topics.pinned') ?></span><?php endif; ?><?php if (($topic['kind'] ?? '') === 'announcement'): ?><span class="admin-badge"><?= quaeriva_t($translator, 'topics.announcement_badge') ?></span><?php endif; ?><?php if (($topic['is_locked'] ?? 0) == 1): ?><span><?= quaeriva_t($translator, 'topics.locked') ?></span><?php endif; ?></div>
    <h1 dir="<?= quaeriva_escape($contentDirection) ?>" itemprop="headline"><?= quaeriva_escape($topic['title'] ?? '') ?></h1>
    <div class="question-meta question-meta-large"><?php if ($authorName !== '' && $username !== ''): ?><a class="author-chip" href="<?= quaeriva_escape(quaeriva_url('u/' . rawurlencode($username))) ?>"><?php if ($avatar !== ''): ?><img src="<?= quaeriva_escape($avatar) ?>" alt="" width="36" height="36" loading="lazy" decoding="async"><?php endif; ?><span><bdi><?= quaeriva_escape($authorName) ?></bdi></span></a><?php elseif ($authorName !== ''): ?><span class="author-chip author-chip-static"><bdi><?= quaeriva_escape($authorName) ?></bdi></span><?php else: ?><span><?= quaeriva_t($translator, 'common.anonymous') ?></span><?php endif; ?><span><bdi><?= (int) ($topic['views_count'] ?? 0) ?></bdi> <?= quaeriva_t($translator, 'common.views') ?></span><span><bdi><?= (int) ($topic['comments_count'] ?? $comments['total'] ?? 0) ?></bdi> <?= quaeriva_t($translator, 'topics.comments') ?></span></div>
    <div class="rich-text" dir="<?= quaeriva_escape($contentDirection) ?>" itemprop="text"><?= quaeriva_rich_text($topic['body'] ?? '', 'full') ?></div>
    <div class="action-row question-action-bar" aria-label="<?= quaeriva_t($translator, 'topics.actions') ?>">
      <?php if ($currentUser !== null): ?><?php if (!empty($reportedItems['topic'])): ?><span class="report-done"><?= quaeriva_t($translator, 'topics.report_done') ?></span><?php else: ?><details class="report-action"><summary class="button button-small button-secondary action-button" aria-label="<?= quaeriva_t($translator, 'topics.report') ?>" title="<?= quaeriva_t($translator, 'topics.report') ?>"><?= quaeriva_icon('flag') ?><span class="action-button-label"><?= quaeriva_t($translator, 'topics.report') ?></span></summary><form class="compact-form report-popover" method="post" action="<?= quaeriva_escape($viewUrl . '/report') ?>"><input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken) ?>"><input type="hidden" name="comments_page" value="<?= (int) ($comments['page'] ?? 1) ?>"><select name="reason"><?php foreach ($reportReasons as $reason): ?><option value="<?= quaeriva_escape($reason) ?>"><?= quaeriva_t($translator, 'questions.report_reasons.' . $reason) ?></option><?php endforeach; ?></select><textarea name="notes" rows="2" maxlength="1000"></textarea><button class="button button-small" type="submit"><?= quaeriva_t($translator, 'questions.report_submit') ?></button></form></details><?php endif; ?><?php else: ?><a class="button button-small button-secondary action-button" href="<?= quaeriva_escape(quaeriva_url('login', ['redirect' => $currentPath ?? $viewUrl])) ?>" aria-label="<?= quaeriva_t($translator, 'topics.report') ?>" title="<?= quaeriva_t($translator, 'topics.report') ?>"><?= quaeriva_icon('flag') ?><span class="action-button-label"><?= quaeriva_t($translator, 'topics.report') ?></span></a><?php endif; ?>
      <?php if (($site['follows_enabled'] ?? true) === true): ?><?php if ($currentUser !== null): ?><form class="follow-form" method="post" action="<?= quaeriva_escape($viewUrl . '/follow') ?>"><input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken) ?>"><button class="button button-small action-button <?= !empty($isFollowing) ? 'button-active' : 'button-secondary' ?>" type="submit" aria-label="<?= quaeriva_t($translator, !empty($isFollowing) ? 'topics.unfollow' : 'topics.follow') ?>" title="<?= quaeriva_t($translator, !empty($isFollowing) ? 'topics.unfollow' : 'topics.follow') ?>"><?= quaeriva_icon('bookmark') ?><span class="action-button-label"><?= quaeriva_t($translator, !empty($isFollowing) ? 'topics.unfollow' : 'topics.follow') ?></span></button></form><?php else: ?><a class="button button-small button-secondary action-button" href="<?= quaeriva_escape(quaeriva_url('login', ['redirect' => $currentPath ?? $viewUrl])) ?>" aria-label="<?= quaeriva_t($translator, 'topics.follow') ?>" title="<?= quaeriva_t($translator, 'topics.follow') ?>"><?= quaeriva_icon('bookmark') ?><span class="action-button-label"><?= quaeriva_t($translator, 'topics.follow') ?></span></a><?php endif; ?><?php endif; ?>
      <button class="button button-small button-secondary action-button" type="button" data-share-topic data-share-url="<?= quaeriva_escape($currentPath ?? $viewUrl) ?>" aria-label="<?= quaeriva_t($translator, 'questions.share') ?>" title="<?= quaeriva_t($translator, 'questions.share') ?>"><?= quaeriva_icon('share') ?><span class="action-button-label"><?= quaeriva_t($translator, 'questions.share') ?></span></button>
    </div>
  </article>
  <?php if (($site['comments_enabled'] ?? true) === true): ?>
  <section class="question-comments compact-comment-thread topic-thread" id="comments" aria-labelledby="comments-heading"><div class="section-heading compact"><h2 id="comments-heading"><?= quaeriva_t($translator, 'topics.comment_count', ['count' => (int) ($comments['total'] ?? 0)]) ?></h2></div>
    <?php if (($comments['comments'] ?? []) !== []): ?><div class="comment-list compact-comment-list"><?php foreach ((array) $comments['comments'] as $comment): $commentId = (int) ($comment['id'] ?? 0); $commentReported = in_array($commentId, (array) ($reportedItems['comment'] ?? []), true); ?><article class="comment-item" id="comment-<?= $commentId ?>"><div class="comment-body" dir="<?= quaeriva_escape($contentDirection) ?>"><?= quaeriva_rich_text($comment['body'] ?? '', 'comment') ?></div><footer class="comment-meta"><small><bdi><?= quaeriva_escape(trim((string) ($comment['author_name'] ?? '')) ?: $translator->get('common.anonymous')) ?></bdi></small><?php if ($currentUser !== null && $commentId > 0): ?><?php if ($commentReported): ?><span class="report-done"><?= quaeriva_t($translator, 'topics.report_done') ?></span><?php else: ?><details class="report-action"><summary class="action-button" aria-label="<?= quaeriva_t($translator, 'questions.report_comment') ?>" title="<?= quaeriva_t($translator, 'questions.report_comment') ?>"><?= quaeriva_icon('flag') ?><span class="action-button-label"><?= quaeriva_t($translator, 'questions.report_comment') ?></span></summary><form class="compact-form report-popover" method="post" action="<?= quaeriva_escape($viewUrl . '/comments/' . $commentId . '/report') ?>"><input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken) ?>"><input type="hidden" name="comments_page" value="<?= (int) ($comments['page'] ?? 1) ?>"><select name="reason"><?php foreach ($reportReasons as $reason): ?><option value="<?= quaeriva_escape($reason) ?>"><?= quaeriva_t($translator, 'questions.report_reasons.' . $reason) ?></option><?php endforeach; ?></select><textarea name="notes" rows="2" maxlength="1000"></textarea><button class="button button-small button-secondary" type="submit"><?= quaeriva_t($translator, 'questions.report_submit') ?></button></form></details><?php endif; ?><?php endif; ?></footer></article><?php endforeach; ?></div><?php else: ?><p class="notice notice-muted"><?= quaeriva_t($translator, 'topics.no_comments') ?></p><?php endif; ?>
    <?php if (($topic['is_locked'] ?? 0) == 1): ?><p class="notice notice-muted"><?= quaeriva_t($translator, 'topics.locked_hint') ?></p><?php elseif ($currentUser !== null): ?><details class="comment-composer"<?= (($query['commented'] ?? '') === '1') ? ' open' : '' ?>><summary class="text-link"><?= quaeriva_t($translator, 'topics.add_comment') ?></summary><form class="comment-form compact-comment-form" method="post" action="<?= quaeriva_escape($viewUrl . '/comments') ?>"><input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken) ?>"><?= quaeriva_bot_guard_fields() ?><input type="hidden" name="comments_page" value="<?= (int) ($comments['page'] ?? 1) ?>"><textarea class="comment-textarea" name="body" rows="2" dir="<?= quaeriva_escape($contentDirection) ?>"<?= quaeriva_rich_editor_attributes($translator, 'comment', '') ?><?= $commentMax > 0 ? ' maxlength="' . $commentMax . '"' : '' ?><?= $commentMin > 0 ? ' minlength="' . $commentMin . '"' : '' ?> required placeholder="<?= quaeriva_t($translator, 'topics.comment_placeholder') ?>"></textarea><button class="button button-small button-secondary" type="submit"><?= quaeriva_t($translator, 'topics.comment') ?></button></form></details><?php endif; ?>
    <?php $page = max(1, (int) ($comments['page'] ?? 1)); $pages = max(1, (int) ($comments['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_topic_url($slug, ['comments_page' => $page - 1])) ?>#comments"><?= quaeriva_t($translator, 'topics.previous_page') ?></a><?php endif; ?><?php if ($page < $pages): ?><a class="button button-small button-secondary" href="<?= quaeriva_escape(quaeriva_topic_url($slug, ['comments_page' => $page + 1])) ?>#comments"><?= quaeriva_t($translator, 'topics.next_page') ?></a><?php endif; ?></nav><?php endif; ?>
  </section>
  <?php endif; ?>
<?php endif; ?>
</div></section>
