{# templates/_search_results_table.html #}
{# This partial template renders the search results table and pagination #}

{% if packages %}
    <table class="table table-striped table-hover">
        <thead>
            <tr>
                <th scope="col">Package</th>
                <th scope="col">Latest</th>
                <th scope="col">Author</th>
                <th scope="col">FHIR</th>
                <th scope="col">Versions</th>
                <!-- <th scope="col">Canonical</th> -->
                {# Add Dependencies header if needed based on your data #}
                {# <th scope="col">Dependencies</th> #}
            </tr>
        </thead>
        <tbody>
            {% for pkg in packages %}
            <tr>
                <td>
                    <i class="bi bi-box-seam me-2"></i>
                    {# Link to package details page - adjust if endpoint name is different #}
                    <a class="text-primary"
                       href="{{ url_for('package_details_view', name=pkg.name) }}"
                       {# Optional: Add HTMX GET for inline details if desired #}
                       {# hx-get="{{ url_for('package_details', name=pkg.name) }}" #}
                       {# hx-target="#search-results" #}
                       >
                       {{ pkg.name }}
                    </a>
                </td>
                <td>{{ pkg.display_version }}</td>
                <td>{{ pkg.author or '' }}</td>
                <td>{{ pkg.fhir_version or '' }}</td>
                <td>{{ pkg.version_count }}</td>
                <!-- <td>{{ pkg.canonical or '' }}</td> -->
                {# Add Dependencies data if needed #}
                {# <td>{{ pkg.dependencies | join(', ') if pkg.dependencies else '' }}</td> #}
            </tr>
            {% endfor %}
        </tbody>
    </table>

    {# Pagination Controls - Ensure 'pagination' object is passed from the route #}
    {% if pagination and pagination.pages > 1 %}
    <nav aria-label="Page navigation">
        <ul class="pagination justify-content-center">
            {# Previous Page Link #}
            {% if pagination.has_prev %}
            <li class="page-item">
                {# Use hx-get for HTMX-powered pagination within the results area #}
                <a class="page-link"
                   href="{{ url_for('search_and_import', page=pagination.prev_num, search=request.args.get('search', '')) }}" {# Keep standard link for non-JS fallback #}
                   hx-get="{{ url_for('api_search_packages', page=pagination.prev_num, search=request.args.get('search', '')) }}" {# HTMX target #}
                   hx-target="#search-results"
                   hx-indicator=".htmx-indicator"
                   aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
            {% else %}
            <li class="page-item disabled">
                <span class="page-link">&laquo;</span>
            </li>
            {% endif %}

            {# Page Number Links #}
            {% for p in pagination.iter_pages %} {# Iterate over the pre-computed list #}
                {% if p %}
                    <li class="page-item {% if p == pagination.page %}active{% endif %}">
                         <a class="page-link"
                            href="{{ url_for('search_and_import', page=p, search=request.args.get('search', '')) }}"
                            hx-get="{{ url_for('api_search_packages', page=p, search=request.args.get('search', '')) }}"
                            hx-target="#search-results"
                            hx-indicator=".htmx-indicator">
                            {{ p }}
                         </a>
                    </li>
                {% else %}
                    {# Ellipsis for skipped pages #}
                    <li class="page-item disabled"><span class="page-link">...</span></li>
                {% endif %}
            {% endfor %}

            {# Next Page Link #}
            {% if pagination.has_next %}
            <li class="page-item">
                 <a class="page-link"
                    href="{{ url_for('search_and_import', page=pagination.next_num, search=request.args.get('search', '')) }}"
                    hx-get="{{ url_for('api_search_packages', page=pagination.next_num, search=request.args.get('search', '')) }}"
                    hx-target="#search-results"
                    hx-indicator=".htmx-indicator"
                    aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
             {% else %}
            <li class="page-item disabled">
                <span class="page-link">&raquo;</span>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %} {# End pagination nav #}

{% elif request and request.args.get('search') %}
    {# Message when search term is present but no results found #}
     <p class="text-muted text-center mt-3">No packages found matching your search term.</p>
{% else %}
    {# Initial message before any search #}
    <p class="text-muted text-center mt-3">Start typing in the search box above to find packages.</p>
{% endif %}
