<?php ob_start(); ?>

<section class="search-section">
    <div class="container">
        <h1 class="section-title"><?= e($page_title ?? __('theme.search_title')) ?></h1>

        <!-- Search Form -->
        <form class="search-form" action="/search" method="get" role="search">
            <div class="search-input-group">
                <input type="search" name="q" value="<?= e($query ?? '') ?>"
                       placeholder="<?= e(__('theme.search_placeholder')) ?>"
                       class="search-input" autocomplete="off" autofocus
                       minlength="2" maxlength="200" aria-label="<?= e(__('theme.search_placeholder')) ?>">
                <button type="submit" class="search-submit" aria-label="<?= e(__('theme.search_button')) ?>">
                    <?= render_icon('search', 20) ?>
                    <span><?= e(__('theme.search_button')) ?></span>
                </button>
            </div>
        </form>

        <?php if (!empty($error_message)): ?>
        <p class="search-error"><?= e($error_message) ?></p>
        <?php endif; ?>

        <?php if (!empty($query) && empty($error_message)): ?>

            <?php if ($total_results > 0): ?>
            <p class="search-result-count">
                <?= $total_results === 1
                    ? e(__('theme.search_result_count', ['count' => 1]))
                    : e(__('theme.search_result_count', ['count' => $total_results])) ?>
            </p>

            <div class="search-results">
                <?php foreach ($results as $result): ?>
                <article class="search-result-card">
                    <div class="search-result-meta">
                        <span class="search-result-type search-result-type--<?= e($result['type']) ?>">
                            <?= render_icon($result['icon'], 14) ?>
                            <?= e(__('theme.search_type_' . $result['type'])) ?>
                        </span>
                        <?php if (!empty($result['date'])): ?>
                        <span class="search-result-date"><?= format_date($result['date']) ?></span>
                        <?php endif; ?>
                    </div>
                    <h2 class="search-result-title">
                        <a href="<?= e($result['url']) ?>"><?= highlight_search(e($result['title']), $query) ?></a>
                    </h2>
                    <?php if (!empty($result['excerpt'])): ?>
                    <p class="search-result-excerpt"><?= highlight_search(e($result['excerpt']), $query) ?></p>
                    <?php endif; ?>
                    <a href="<?= e($result['url']) ?>" class="search-result-link"><?= e($result['url']) ?></a>
                </article>
                <?php endforeach; ?>
            </div>

            <?php if (!empty($pagination) && $pagination['total_pages'] > 1): ?>
            <nav class="blog-pagination" aria-label="Search page navigation">
                <?php
                    $p    = $pagination;
                    $base = $p['base_url'];
                    $sep  = str_contains($base, '?') ? '&' : '?';
                ?>
                <?php if ($p['current_page'] > 1): ?>
                <a href="<?= e($base . $sep . 'page=' . ($p['current_page'] - 1)) ?>" class="pagination-link" rel="prev">&larr;</a>
                <?php else: ?>
                <span class="pagination-link disabled">&larr;</span>
                <?php endif; ?>

                <span class="pagination-info"><?= $p['current_page'] ?> / <?= $p['total_pages'] ?></span>

                <?php if ($p['current_page'] < $p['total_pages']): ?>
                <a href="<?= e($base . $sep . 'page=' . ($p['current_page'] + 1)) ?>" class="pagination-link" rel="next">&rarr;</a>
                <?php else: ?>
                <span class="pagination-link disabled">&rarr;</span>
                <?php endif; ?>
            </nav>
            <?php endif; ?>

            <?php else: ?>
            <div class="search-no-results">
                <?= render_icon('search', 48) ?>
                <p><?= e(__('theme.search_no_results', ['query' => $query])) ?></p>
                <p class="text-muted"><?= e(__('theme.search_no_results_hint')) ?></p>
            </div>
            <?php endif; ?>

        <?php endif; ?>
    </div>
</section>

<?php $content = ob_get_clean(); include __DIR__ . '/layout.php'; ?>
