{# HTMX partial — replaces the entire <tr> for a job row while it's running.
   Stops polling once the job is complete or failed (hx-trigger="every 2s" only applies
   while this partial keeps returning a polling trigger). #}

{% set pct = [((job.progress_current / job.progress_total) * 100) | int, 100] | min if job.progress_total else 0 %}

<tr id="job-{{ job.id }}"
    {% if job.status == 'pending' and not job.error %}
    hx-get="{{ url_for('pseo.pseo_job_status', job_id=job.id) }}"
    hx-trigger="every 2s"
    hx-target="this"
    hx-swap="outerHTML"
    {% endif %}>
  <td class="text-xs text-slate">#{{ job.id }}</td>
  <td>—</td>{# payload not re-fetched in status endpoint — static display #}
  <td>
    {% if job.status == 'complete' %}
      <span class="badge-success">Complete</span>
    {% elif job.status == 'failed' %}
      <span class="badge-danger">Failed</span>
    {% else %}
      <span class="badge-warning">Running…</span>
    {% endif %}
  </td>
  <td>
    {% if job.progress_total and job.progress_total > 0 %}
    <div class="flex items-center gap-2">
      <div class="progress-bar-wrap" style="min-width:120px;">
        <div class="progress-bar-fill" style="width: {{ pct }}%"></div>
      </div>
      <span class="text-xs text-slate">{{ job.progress_current }}/{{ job.progress_total }}</span>
    </div>
    {% else %}—{% endif %}
  </td>
  <td class="text-xs text-slate">{{ (job.created_at or '') | truncate(19, True, '') }}</td>
  <td class="text-xs text-slate">{{ (job.completed_at or '') | truncate(19, True, '') }}</td>
  <td>
    {% if job.error %}
      <details>
        <summary class="text-xs text-red-600 cursor-pointer">Error</summary>
        <pre class="text-xs mt-1 p-2 bg-gray-50 rounded overflow-auto max-w-xs">{{ job.error[:500] }}</pre>
      </details>
    {% else %}—{% endif %}
  </td>
</tr>
