{% extends "base.html" %}
{% block title %}Pre-update backups{% endblock %}

{% block head %}
{{ super() if super }}
<style>
  .bk-banner { background: linear-gradient(135deg,#243b6a,#3a5fb2); color:#fff; border-radius:.75rem; padding:1.25rem 1.5rem; margin-bottom:1.25rem; box-shadow:0 2px 12px rgba(0,0,0,.25); }
  .bk-banner h1 { margin: 0; font-weight: 600; }
  .bk-card { border-radius:.75rem; box-shadow:0 1px 6px rgba(0,0,0,.08); }
  .bk-meta { color:#6c757d; font-size:.85rem; }
  .bk-files code { background:#f1f3f5; padding: 0 .3rem; border-radius: .2rem; margin-right:.2rem; font-size:.78rem; }
</style>
{% endblock %}

{% block content %}
<div class="container py-3">
  <div class="bk-banner">
    <h1 class="m-0"><i class="bi bi-clock-history"></i> Pre-update backups</h1>
    <div class="text-white-50 mt-1">
      Snapshots taken automatically by <code>update.sh</code> before each
      upgrade. Restore <code>.env</code> or the production DB from any
      one of them.
    </div>
  </div>

  {% if not restore_available %}
    <div class="alert alert-warning">
      The restore helper isn't installed at <code>{{ helper_path }}</code>.
      Delete is available; Restore buttons need <code>deploy/run_restore.sh</code>
      plus the sudoers grant for it. Re-run <code>update.sh</code> on
      this release to install both.
    </div>
  {% endif %}

  {% if not rows %}
    <div class="alert alert-info">
      No backups on disk yet. Backups land at
      <code>/tmp/anetbbs-backup-*</code> the moment you start the next
      upgrade. They survive reboots only if your distro persists
      <code>/tmp</code> across boots (most don't).
    </div>
  {% else %}
  <div class="card bk-card">
    <table class="table table-hover align-middle mb-0">
      <thead class="table-light">
        <tr>
          <th>Created</th><th>Version change</th><th>Size</th>
          <th>Files</th><th class="text-end">Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for r in rows %}
        <tr>
          <td>
            <strong>{{ r.created_str }}</strong><br>
            <span class="bk-meta">{{ r.name }}</span>
          </td>
          <td>
            <code>{{ r.from_version }}</code>
            <i class="bi bi-arrow-right"></i>
            <code>{{ r.to_version }}</code>
          </td>
          <td>{{ r.size_human }}</td>
          <td class="bk-files">
            {% for f in r.files %}<code>{{ f }}</code>{% endfor %}
          </td>
          <td class="text-end">
            {% if restore_available %}
              {% if '.env.bak' in r.files %}
              <form method="POST" action="{{ url_for('backups_admin.restore_env', name=r.name) }}" style="display:inline"
                    onsubmit="return confirm('Replace the live .env with the snapshot from {{ r.created_str }}? Restart anetbbs-web after.');">
                {% if csrf_token %}<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">{% endif %}
                <button class="btn btn-sm btn-outline-warning" title="Restore .env">
                  <i class="bi bi-arrow-counterclockwise"></i> .env
                </button>
              </form>
              {% endif %}
              {% if 'anetbbs.db.bak' in r.files %}
              <form method="POST" action="{{ url_for('backups_admin.restore_db', name=r.name) }}" style="display:inline"
                    onsubmit="return confirm('Replace the live production DB with the snapshot from {{ r.created_str }}? anetbbs-web will restart. ALL changes since then will be LOST.');">
                {% if csrf_token %}<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">{% endif %}
                <button class="btn btn-sm btn-outline-danger" title="Restore DB">
                  <i class="bi bi-arrow-counterclockwise"></i> DB
                </button>
              </form>
              {% endif %}
            {% endif %}
            <form method="POST" action="{{ url_for('backups_admin.delete', name=r.name) }}" style="display:inline"
                  onsubmit="return confirm('Delete backup {{ r.name }} permanently?');">
              {% if csrf_token %}<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">{% endif %}
              <button class="btn btn-sm btn-outline-secondary" title="Delete">
                <i class="bi bi-trash"></i>
              </button>
            </form>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
    <div class="card-footer bk-meta">
      Backups older than a week are usually safe to delete unless
      you suspect data corruption that long ago. The DB
      backup is a consistent SQLite snapshot
      (sqlite3 <code>.backup</code>), safe to restore in-place.
    </div>
  </div>
  {% endif %}
</div>
{% endblock %}
