<?php
declare(strict_types=1);
$filters = is_array($results['filters'] ?? null) ? $results['filters'] : [];
$items = is_array($results['items'] ?? null) ? $results['items'] : [];
$total = (int) ($results['total'] ?? 0);
$page = max(1, (int) ($results['page'] ?? 1));
$perPage = max(1, (int) ($results['per_page'] ?? 20));
$pages = max(1, (int) ceil($total / $perPage));
?>
<section class="content-section search-page">
    <div class="container">
        <div class="section-heading search-heading">
            <div><p class="eyebrow"><?= quaeriva_t($translator, 'search.eyebrow') ?></p><h1><?= quaeriva_t($translator, 'search.title') ?></h1></div>
            <span class="search-count"><bdi><?= quaeriva_escape($total) ?></bdi> <?= quaeriva_t($translator, 'search.results') ?></span>
        </div>
        <form class="search-filter-card" method="get" action="<?= quaeriva_escape(quaeriva_url('search')) ?>" role="search">
            <div class="search-query-row">
                <label class="visually-hidden" for="search-q"><?= quaeriva_t($translator, 'common.search') ?></label>
                <input id="search-q" type="search" name="q" dir="<?= quaeriva_escape($localeContext->htmlAttributes()['dir']) ?>" value="<?= quaeriva_escape($filters['q'] ?? '') ?>" placeholder="<?= quaeriva_t($translator, 'common.search_placeholder') ?>">
                <button class="button" type="submit"><?= quaeriva_t($translator, 'common.search_submit') ?></button>
            </div>
            <div class="search-filter-grid">
                <label><span><?= quaeriva_t($translator, 'search.category') ?></span><select name="category"><option value=""><?= quaeriva_t($translator, 'search.any') ?></option><?php foreach (($options['categories'] ?? []) as $category): ?><option value="<?= quaeriva_escape($category['slug'] ?? '') ?>"<?= ($filters['category'] ?? '') === (string) ($category['slug'] ?? '') ? ' selected' : '' ?>><?= quaeriva_escape($category['name'] ?? '') ?></option><?php endforeach; ?></select></label>
                <label><span><?= quaeriva_t($translator, 'search.tag') ?></span><select name="tag"><option value=""><?= quaeriva_t($translator, 'search.any') ?></option><?php foreach (($options['tags'] ?? []) as $tag): ?><option value="<?= quaeriva_escape($tag['slug'] ?? '') ?>"<?= ($filters['tag'] ?? '') === (string) ($tag['slug'] ?? '') ? ' selected' : '' ?>><?= quaeriva_escape($tag['name'] ?? '') ?></option><?php endforeach; ?></select></label>
                <label><span><?= quaeriva_t($translator, 'search.answered') ?></span><select name="answered"><option value=""><?= quaeriva_t($translator, 'search.any') ?></option><option value="answered"<?= ($filters['answered'] ?? '') === 'answered' ? ' selected' : '' ?>><?= quaeriva_t($translator, 'search.answered_only') ?></option><option value="unanswered"<?= ($filters['answered'] ?? '') === 'unanswered' ? ' selected' : '' ?>><?= quaeriva_t($translator, 'search.unanswered_only') ?></option></select></label>
                <label><span><?= quaeriva_t($translator, 'search.date') ?></span><select name="date"><option value=""><?= quaeriva_t($translator, 'search.any_time') ?></option><option value="7d"<?= ($filters['date'] ?? '') === '7d' ? ' selected' : '' ?>><?= quaeriva_t($translator, 'search.last_7_days') ?></option><option value="30d"<?= ($filters['date'] ?? '') === '30d' ? ' selected' : '' ?>><?= quaeriva_t($translator, 'search.last_30_days') ?></option><option value="365d"<?= ($filters['date'] ?? '') === '365d' ? ' selected' : '' ?>><?= quaeriva_t($translator, 'search.last_year') ?></option></select></label>
                <label><span><?= quaeriva_t($translator, 'search.member') ?></span><input name="member" dir="ltr" value="<?= quaeriva_escape($filters['member'] ?? '') ?>" placeholder="username"></label>
                <label><span><?= quaeriva_t($translator, 'search.content_language') ?></span><select name="locale"><option value=""><?= quaeriva_t($translator, 'search.any') ?></option><?php foreach (($site['languages'] ?? []) as $language): $code=(string)($language['code']??''); ?><option value="<?= quaeriva_escape($code) ?>"<?= ($filters['locale'] ?? '') === $code ? ' selected' : '' ?>><?= quaeriva_escape($language['native_name'] ?? $language['name'] ?? $code) ?></option><?php endforeach; ?></select></label>
            </div>
            <div class="search-filter-actions"><a class="text-link" href="<?= quaeriva_escape(quaeriva_url('search')) ?>"><?= quaeriva_t($translator, 'search.clear') ?></a></div>
        </form>

        <?php if (!$dbAvailable): ?><p class="notice notice-muted"><?= quaeriva_t($translator, 'common.database_unavailable') ?></p><?php elseif ($items === []): ?><div class="empty-state"><span class="empty-mark" aria-hidden="true">⌕</span><h2><?= quaeriva_t($translator, 'common.no_search_results') ?></h2><p><?= quaeriva_t($translator, 'search.try_filters') ?></p></div><?php else: ?>
            <div class="question-list search-results-list">
            <?php foreach ($items as $question): ?>
                <article class="question-card">
                    <div class="question-stats"><span><strong><bdi><?= quaeriva_escape($question['votes_count'] ?? 0) ?></bdi></strong><?= quaeriva_t($translator, 'common.votes') ?></span><span class="stat-answer"><strong><bdi><?= quaeriva_escape($question['answers_count'] ?? 0) ?></bdi></strong><?= quaeriva_t($translator, 'common.answers') ?></span></div>
                    <div class="question-card-body"><h2><a href="<?= quaeriva_escape(quaeriva_question_url((string)($question['slug'] ?? ''), (int)($question['id'] ?? 0))) ?>" dir="auto"><?= quaeriva_escape($question['title'] ?? '') ?></a></h2><p dir="auto"><?= quaeriva_plain_text($question['body'] ?? '') ?></p><div class="question-meta"><?php if (($question['category_slug'] ?? '') !== ''): ?><a class="text-link" href="<?= quaeriva_escape(quaeriva_url('c/' . rawurlencode((string)$question['category_slug']))) ?>"><?= quaeriva_escape($question['category_name'] ?? '') ?></a><?php endif; ?><?php if (($question['author_username'] ?? '') !== ''): ?><a class="text-link" href="<?= quaeriva_escape(quaeriva_url('u/' . rawurlencode((string)$question['author_username']))) ?>"><?= quaeriva_escape($question['author_name'] ?? '') ?></a><?php else: ?><span><?= quaeriva_escape(($question['author_name'] ?? '') !== '' ? $question['author_name'] : $translator->get('common.anonymous')) ?></span><?php endif; ?></div></div>
                </article>
            <?php endforeach; ?>
            </div>
            <?php if ($pages > 1): ?><nav class="pagination" aria-label="<?= quaeriva_t($translator, 'search.pagination') ?>"><?php for ($i=1;$i<=$pages;$i++): $params=array_filter(array_merge($filters,['page'=>$i]),static fn($v)=>$v!==''&&$v!==null); ?><a href="<?= quaeriva_escape(quaeriva_url('search',$params)) ?>"<?= $i===$page ? ' aria-current="page"' : '' ?>><?= quaeriva_escape($i) ?></a><?php endfor; ?></nav><?php endif; ?>
        <?php endif; ?>
    </div>
</section>
