{% set page = page | default(1) %}
{% set per_page = per_page | default(50) %}
{% set total = total | default(0) %}
{% set start = (page - 1) * per_page + 1 %}
{% set end = [page * per_page, total] | min %}
{% set has_prev = page > 1 %}
{% set has_next = (page * per_page) < total %}

{% macro heat_badge(score) %}
  <span class="lead-heat-{{ score or 'cool' }}">{{ (score or 'cool') | upper }}</span>
{% endmacro %}

{% macro status_badge(status) %}
  <span class="lead-status-badge lead-status-{{ status | replace(' ', '_') }}">{{ status | replace('_', ' ') }}</span>
{% endmacro %}

{# Hidden inputs carry current filters for pagination hx-include #}
<div id="lead-filter-state" style="display:none">
  <input type="hidden" name="status"  value="{{ current_status | default('') }}">
  <input type="hidden" name="heat"    value="{{ current_heat | default('') }}">
  <input type="hidden" name="country" value="{{ current_country | default('') }}">
  <input type="hidden" name="search"  value="{{ current_search | default('') }}">
  <input type="hidden" name="days"    value="{{ current_days | default('') }}">
</div>

{% if leads %}
<div class="card">
  <div style="overflow-x:auto">
  <table class="table">
    <thead>
      <tr>
        <th style="width:32px"><input type="checkbox" id="lead-select-all" onchange="document.querySelectorAll('.lead-checkbox').forEach(cb => { cb.checked = this.checked; toggleLeadSelect(Number(cb.dataset.id), this.checked); })"></th>
        <th>ID</th>
        <th>Heat</th>
        <th>Contact</th>
        <th>Country</th>
        <th>Courts</th>
        <th>Budget</th>
        <th>Status</th>
        <th>Unlocks</th>
        <th>Created</th>
      </tr>
    </thead>
    <tbody>
      {% for lead in leads %}
      <tr data-href="{{ url_for('admin.lead_detail', lead_id=lead.id) }}">
        <td onclick="event.stopPropagation()">
          <input type="checkbox" class="lead-checkbox" data-id="{{ lead.id }}"
                 onchange="toggleLeadSelect({{ lead.id }}, this.checked)">
        </td>
        <td><a href="{{ url_for('admin.lead_detail', lead_id=lead.id) }}">#{{ lead.id }}</a></td>
        <td>{{ heat_badge(lead.heat_score) }}</td>
        <td>
          <span class="text-sm">{{ lead.contact_name or '-' }}</span><br>
          <span class="text-xs text-slate">{{ lead.contact_email or '-' }}</span>
        </td>
        <td>{{ lead.country or '-' }}</td>
        <td>{{ lead.court_count or '-' }}</td>
        <td>{% if lead.budget_estimate %}€{{ "{:,}".format(lead.budget_estimate | int) }}{% else %}-{% endif %}</td>
        <td>{{ status_badge(lead.status) }}</td>
        <td>{{ lead.unlock_count or 0 }}</td>
        <td class="mono text-sm">{{ lead.created_at[:10] if lead.created_at else '-' }}</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
  </div>
</div>

<!-- Pagination -->
{% if total > per_page %}
<div class="flex justify-between items-center mt-4" style="font-size:0.8125rem; color:#64748B;">
  <span>Showing {{ start }}–{{ end }} of {{ total }}</span>
  <div class="flex gap-2">
    {% if has_prev %}
    <button class="btn-outline btn-sm"
            hx-get="{{ url_for('admin.lead_results') }}"
            hx-vals='{"page": "{{ page - 1 }}"}'
            hx-include="#lead-filter-state"
            hx-target="#lead-results"
            hx-swap="innerHTML">
      &larr; Prev
    </button>
    {% endif %}
    <span>Page {{ page }}</span>
    {% if has_next %}
    <button class="btn-outline btn-sm"
            hx-get="{{ url_for('admin.lead_results') }}"
            hx-vals='{"page": "{{ page + 1 }}"}'
            hx-include="#lead-filter-state"
            hx-target="#lead-results"
            hx-swap="innerHTML">
      Next &rarr;
    </button>
    {% endif %}
  </div>
</div>
{% else %}
<p class="text-xs text-slate mt-3">Showing all {{ total }} results</p>
{% endif %}

{% else %}
<div class="card text-center" style="padding:2rem">
  <p class="text-slate">No leads match the current filters.</p>
</div>
{% endif %}
