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

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

{% block admin_content %}
  <!-- Failed Tasks -->
  {% if failed_tasks %}
  <section class="mb-8">
    <h2 class="text-xl mb-4" style="color:#EF4444;">Failed Tasks ({{ failed_tasks | length }})</h2>
    <div class="card" style="padding:0;overflow:hidden;border-color:rgba(239,68,68,0.3);">
      <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 px-1 rounded" style="background:#F5F0EB;">{{ task.task_name }}</code></td>
              <td>
                <details>
                  <summary class="cursor-pointer text-xs text-stone">{{ task.error[:40] if task.error else 'No error' }}...</summary>
                  <pre class="text-xs mt-2 whitespace-pre-wrap text-stone-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" style="color:#EF4444;">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" style="padding:0;overflow:hidden;">
      {% 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 px-1 rounded" style="background:#F5F0EB;">{{ 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-stone text-sm" style="padding:16px;">No tasks in queue.</p>
      {% endif %}
    </div>
  </section>
{% endblock %}
