{% extends "admin/base_admin.html" %}
{% set admin_page = "tasks" %}

{% block title %}Tasks - Admin - {{ config.APP_NAME }}{% endblock %}

{% block admin_content %}
  <header class="flex justify-between items-center mb-8">
    <h1 class="text-2xl">Task Queue</h1>
    <a href="{{ url_for('admin.index') }}" class="btn-outline btn-sm">&larr; Dashboard</a>
  </header>

  <!-- Failed Tasks -->
  {% if failed_tasks %}
  <section class="mb-10">
    <h2 class="text-xl text-danger mb-4">Failed Tasks ({{ failed_tasks | length }})</h2>
    <div class="card border-danger/30">
      <div class="overflow-x-auto">
        <table class="table">
          <thead>
            <tr>
              <th>ID</th>
              <th>Task</th>
              <th>Error</th>
              <th>Retries</th>
              <th>Created</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            {% for task in failed_tasks %}
            <tr>
              <td class="mono text-sm">{{ task.id }}</td>
              <td><code class="text-sm bg-soft-white px-1 rounded">{{ task.task_name }}</code></td>
              <td>
                <details>
                  <summary class="cursor-pointer text-xs text-slate">{{ task.error[:40] if task.error else 'No error' }}...</summary>
                  <pre class="text-xs mt-2 whitespace-pre-wrap text-slate-dark">{{ task.error }}</pre>
                </details>
              </td>
              <td class="mono text-sm">{{ task.retries }}</td>
              <td class="mono text-sm">{{ task.created_at[:16] }}</td>
              <td>
                <div class="flex gap-2">
                  <form method="post" action="{{ url_for('admin.task_retry', task_id=task.id) }}" class="m-0">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button type="submit" class="btn-outline btn-sm">Retry</button>
                  </form>
                  <form method="post" action="{{ url_for('admin.task_delete', task_id=task.id) }}" class="m-0">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button type="submit" class="btn-outline btn-sm text-danger">Delete</button>
                  </form>
                </div>
              </td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    </div>
  </section>
  {% endif %}

  <!-- All Tasks -->
  <section>
    <h2 class="text-xl mb-4">Recent Tasks</h2>
    <div class="card">
      {% if tasks %}
      <div class="overflow-x-auto">
        <table class="table">
          <thead>
            <tr>
              <th>ID</th>
              <th>Task</th>
              <th>Status</th>
              <th>Run At</th>
              <th>Created</th>
              <th>Completed</th>
            </tr>
          </thead>
          <tbody>
            {% for task in tasks %}
            <tr>
              <td class="mono text-sm">{{ task.id }}</td>
              <td><code class="text-sm bg-soft-white px-1 rounded">{{ task.task_name }}</code></td>
              <td>
                {% if task.status == 'complete' %}
                <span class="badge-success">complete</span>
                {% elif task.status == 'failed' %}
                <span class="badge-danger">failed</span>
                {% elif task.status == 'pending' %}
                <span class="badge-warning">pending</span>
                {% else %}
                <span class="badge">{{ task.status }}</span>
                {% endif %}
              </td>
              <td class="mono text-sm">{{ task.run_at[:16] if task.run_at else '-' }}</td>
              <td class="mono text-sm">{{ task.created_at[:16] }}</td>
              <td class="mono text-sm">{{ task.completed_at[:16] if task.completed_at else '-' }}</td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
      {% else %}
      <p class="text-slate text-sm">No tasks in queue.</p>
      {% endif %}
    </div>
  </section>
{% endblock %}
