<?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 class="zenith-hero-panel" style="margin-block-end:2rem;">
      <p class="eyebrow"><?= quaeriva_t($translator, 'search.eyebrow') ?></p>
      <h1 style="margin:0.25rem 0 1rem 0; font-size:2rem; 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:36rem;">
        <input name="q" type="search" value="<?= quaeriva_escape($queryStr) ?>" required placeholder="<?= quaeriva_t($translator, 'common.search_placeholder') ?>" style="flex:1;">
        <button class="button" type="submit"><?= quaeriva_t($translator, 'common.search_submit') ?></button>
      </form>
    </div>

    <?php if ($results === []): ?>
      <div class="empty-state">
        <h2><?= quaeriva_t($translator, 'search.no_results') ?></h2>
        <p><?= quaeriva_t($translator, 'search.no_results_hint') ?></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);
          $answers = (int) ($item['answers_count'] ?? 0);
          $comments = (int) ($item['comments_count'] ?? 0);
        ?>
          <article class="zenith-card <?= $isQ ? 'is-question' : 'is-discussion' ?>">
            <div class="zenith-metric-col">
              <div class="zenith-vote-pill"><strong><?= $isQ ? $votes : $comments ?></strong><span><?= quaeriva_t($translator, $isQ ? 'common.votes' : 'topics.comments') ?></span></div>
              <?php if ($isQ): ?>
                <span class="zenith-answers-badge <?= $answers > 0 ? 'has-answers' : '' ?>"><?= $answers ?> <?= quaeriva_t($translator, 'common.answers') ?></span>
              <?php endif; ?>
            </div>
            <div class="zenith-card-body">
              <h3 class="zenith-card-title"><a dir="auto" href="<?= quaeriva_escape($itemUrl) ?>"><?= quaeriva_escape((string) ($item['title'] ?? '')) ?></a></h3>
              <p class="zenith-card-excerpt" dir="auto"><?= quaeriva_escape(mb_strimwidth(strip_tags((string) ($item['body'] ?? '')), 0, 180, '…', 'UTF-8')) ?></p>
            </div>
          </article>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>
</section>
