{% extends "base.html" %}

{% block title %}Search{% if query %}: {{ query }}{% endif %} - {{ site.name }}{% endblock %}

{% block content %}
<div class="max-w-4xl mx-auto">
    <header class="mb-8">
        <h1 class="text-2xl font-bold text-gray-900">Search</h1>
    </header>

    <form action="/search" method="get" class="mb-8">
        <div class="flex gap-2">
            <input type="text" name="q" value="{{ query }}" placeholder="Search..."
                   class="flex-1 px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
            <button type="submit" class="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700">
                Search
            </button>
        </div>
    </form>

    {% if query %}
    <p class="text-gray-600 mb-6">{{ total }} result{% if total != 1 %}s{% endif %} for "{{ query }}"</p>

    <div class="space-y-6">
        {% for post in posts %}
        <article class="bg-white p-6 rounded-lg shadow">
            <h2 class="text-lg font-medium text-gray-900">
                <a href="/{{ post.type }}/{{ post.slug }}" class="hover:text-blue-600">
                    {{ post.title }}
                </a>
            </h2>
            <p class="text-gray-600 mt-2">{{ post.excerpt or (post.body | excerpt) }}</p>
            <time class="text-sm text-gray-500">{{ post.created_at[:10] }}</time>
        </article>
        {% else %}
        <p class="text-gray-500">No results found.</p>
        {% endfor %}
    </div>

    {% if total_pages > 1 %}
    <nav class="mt-8 flex justify-center space-x-4">
        {% if page > 1 %}<a href="?q={{ query }}&page={{ page - 1 }}" class="text-blue-600 hover:underline">&laquo; Prev</a>{% endif %}
        <span class="text-gray-600">Page {{ page }} of {{ total_pages }}</span>
        {% if page < total_pages %}<a href="?q={{ query }}&page={{ page + 1 }}" class="text-blue-600 hover:underline">Next &raquo;</a>{% endif %}
    </nav>
    {% endif %}
    {% endif %}
</div>
{% endblock %}
