{% extends "base.html" %}
{% block title %}Historique — {{ job.name }}{% endblock %}

{% block content %}
<div class="mb-6 flex items-center gap-4">
  <a href="{{ url_for('jobs.index') }}" class="text-gray-400 hover:text-gray-600 text-sm">← Dashboard</a>
  <div>
    <h1 class="text-xl font-bold text-gray-900">{{ job.name }}</h1>
    {% if job.type == 'ynh_app' and job.config_json %}
      {% set _cfg = job.config_json | fromjson %}
      {% set _app_id = _cfg.get('app_id') %}
      {% if _app_id %}
        {% set _label = ynh_app_labels.get(_app_id, '') %}
        {% if _label and _label != _app_id %}
          <span class="text-xs text-gray-500">{{ _label }}</span>
        {% else %}
          <span class="font-mono text-xs text-gray-400">{{ _app_id }}</span>
        {% endif %}
      {% endif %}
    {% endif %}
  </div>
  <span class="bg-gray-100 text-gray-600 text-xs px-2 py-0.5 rounded font-mono">{{ job.type }}</span>
  <span class="text-gray-400 text-sm font-mono">{{ job.cron_expr }}</span>
</div>

<div class="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden">
  <div class="px-6 py-4 border-b border-gray-100">
    <h2 class="text-sm font-semibold text-gray-700">
      Historique des exécutions
      <span class="text-gray-400 font-normal">({{ runs | length }} entrées)</span>
    </h2>
  </div>

  {% if not runs %}
    <div class="px-6 py-10 text-center text-gray-400 text-sm">
      Aucune exécution enregistrée pour ce job.
    </div>
  {% else %}
    <div class="overflow-x-auto">
      <table class="w-full text-sm">
        <thead>
          <tr class="text-xs text-gray-500 uppercase tracking-wide bg-gray-50">
            <th class="px-6 py-3 text-left font-medium">Début</th>
            <th class="px-6 py-3 text-left font-medium">Fin</th>
            <th class="px-6 py-3 text-left font-medium">Durée</th>
            <th class="px-6 py-3 text-left font-medium">Statut</th>
            <th class="px-6 py-3 text-left font-medium">Archive</th>
            <th class="px-6 py-3 text-left font-medium">Taille</th>
            <th class="px-6 py-3 text-left font-medium">Log</th>
            <th class="px-6 py-3 text-left font-medium">Action</th>
          </tr>
        </thead>
        <tbody class="divide-y divide-gray-100">
          {% for run in runs %}
            <tr class="hover:bg-gray-50">
              <td class="px-6 py-3 text-xs text-gray-700 whitespace-nowrap">
                {{ run.started_at.strftime('%d/%m/%Y %H:%M:%S') if run.started_at else '—' }}
              </td>
              <td class="px-6 py-3 text-xs text-gray-500 whitespace-nowrap">
                {{ run.finished_at.strftime('%H:%M:%S') if run.finished_at else '—' }}
              </td>
              <td class="px-6 py-3 text-xs text-gray-500">
                {% if run.duration_seconds is not none %}
                  {% if run.duration_seconds >= 60 %}
                    {{ (run.duration_seconds // 60) }}min {{ run.duration_seconds % 60 }}s
                  {% else %}
                    {{ run.duration_seconds }}s
                  {% endif %}
                {% else %}
                  —
                {% endif %}
              </td>
              <td class="px-6 py-3">
                {% if run.status == 'success' %}
                  <span class="bg-green-100 text-green-700 text-xs font-medium px-2 py-0.5 rounded-full">✓ succès</span>
                {% elif run.status == 'warning' %}
                  <span class="bg-amber-100 text-amber-700 text-xs font-medium px-2 py-0.5 rounded-full">⚠ transfert partiel</span>
                {% elif run.status == 'error' %}
                  <span class="bg-red-100 text-red-700 text-xs font-medium px-2 py-0.5 rounded-full">✗ erreur</span>
                {% elif run.status == 'running' %}
                  <span class="bg-blue-100 text-blue-700 text-xs font-medium px-2 py-0.5 rounded-full animate-pulse">⟳ en cours</span>
                {% else %}
                  <span class="text-gray-400 text-xs">{{ run.status or '—' }}</span>
                {% endif %}
              </td>
              <td class="px-6 py-3 text-xs font-mono text-gray-600">
                {{ run.archive_name or '—' }}
              </td>
              <td class="px-6 py-3 text-xs text-gray-500">
                {{ run.size_human if run.size_bytes else '—' }}
              </td>
              <td class="px-6 py-3">
                {% if run.log_text %}
                  <details>
                    <summary class="cursor-pointer text-xs text-blue-600 hover:underline">Voir</summary>
                    <pre class="mt-2 text-xs bg-gray-900 text-gray-100 p-3 rounded-lg overflow-x-auto max-w-xl whitespace-pre-wrap">{{ run.log_text }}</pre>
                  </details>
                {% else %}
                  <span class="text-gray-300 text-xs">—</span>
                {% endif %}
              </td>
              <td class="px-6 py-3">
                {% if run.status in ('success', 'warning') and run.archive_name %}
                  <div class="flex flex-col gap-1">
                    <a href="{{ url_for('jobs.archive_restore', archive_name=run.archive_name) }}"
                       class="text-xs text-orange-600 hover:text-orange-800 hover:underline whitespace-nowrap">
                      ↩ Restaurer
                    </a>
                    {% for jd in job.job_destinations if jd.dest_type == 'ssh' %}
                      <form method="post" action="{{ url_for('dest.archive_transfer', archive_name=run.archive_name) }}">
                        <input type="hidden" name="destination_id" value="{{ jd.dest_id }}">
                        <button type="submit" class="text-xs text-blue-600 hover:text-blue-800 hover:underline whitespace-nowrap text-left">
                          ↑ {{ jd.label }}
                        </button>
                      </form>
                    {% endfor %}
                  </div>
                {% else %}
                  <span class="text-gray-300 text-xs">—</span>
                {% endif %}
              </td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  {% endif %}
</div>
{% endblock %}
