<?php
declare(strict_types=1);
$searchData = is_array($results ?? null) ? $results : [];
$items = is_array($searchData['items'] ?? null) ? $searchData['items'] : [];
$results = $items; // search passes $results as the envelope; the cards below iterate items
$queryStr = trim((string) (($searchData['filters']['q'] ?? '')));
$totalCount = (int) ($searchData['total'] ?? count($items));
?>
<section class="content-section search-results-section">
  <div class="container search-container">
    <div style="background:var(--color-surface); border:1px solid var(--color-border); border-radius:var(--radius-sm); padding:1.25rem; margin-block-end:1.25rem;">
      <h1 style="margin:0 0 0.75rem 0; font-size:1.5rem; font-weight:800;"><?= quaeriva_t($translator, 'search.results_for', ['query' => $queryStr]) ?></h1>
      <form method="get" action="<?= quaeriva_escape(quaeriva_url('search')) ?>" style="display:flex; gap:0.5rem; max-inline-size:32rem;">
        <input name="q" type="search" value="<?= quaeriva_escape($queryStr) ?>" required placeholder="<?= quaeriva_t($translator, 'common.search_placeholder') ?>" style="flex:1;">
        <button class="button button-small" type="submit"><?= quaeriva_t($translator, 'common.search_submit') ?></button>
      </form>
    </div>

    <?php if ($results === []): ?>
      <div class="empty-state" style="background:var(--color-surface); border:1px solid var(--color-border); border-radius:var(--radius-sm);"><p><?= quaeriva_t($translator, 'search.no_results') ?></p></div>
    <?php else: ?>
      <div class="community-feed">
        <?php foreach ($results as $item):
          $isQ = ($item['type'] ?? 'question') === 'question';
          $itemUrl = $isQ
            ? quaeriva_question_url((string) ($item['slug'] ?? ''), (int) ($item['id'] ?? 0))
            : quaeriva_url('d/' . rawurlencode((string) ($item['slug'] ?? '')));
          $votes = (int) ($item['votes_count'] ?? 0);
          $replies = (int) ($isQ ? ($item['answers_count'] ?? 0) : ($item['comments_count'] ?? 0));
        ?>
          <article class="orbit-card">
            <div class="orbit-vote-rail">
              <span class="vote-icon">▲</span>
              <strong><?= $votes ?></strong>
              <span class="vote-icon">▼</span>
            </div>
            <div class="orbit-card-content">
              <h3 class="orbit-post-title"><a dir="auto" href="<?= quaeriva_escape($itemUrl) ?>"><?= quaeriva_escape((string) ($item['title'] ?? '')) ?></a></h3>
              <p class="orbit-post-excerpt" dir="auto"><?= quaeriva_escape(mb_strimwidth(strip_tags((string) ($item['body'] ?? '')), 0, 180, '…', 'UTF-8')) ?></p>
              <div class="orbit-action-bar">
                <a class="orbit-pill-btn" href="<?= quaeriva_escape($itemUrl) ?>">💬 <bdi><?= $replies ?></bdi> <?= quaeriva_t($translator, $isQ ? 'common.answers' : 'topics.comments') ?></a>
              </div>
            </div>
          </article>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>
</section>
