<h1>Backup &amp; Disaster Recovery</h1>

<% if (forbidden) { %>
  <div class="alert alert-danger">Your role does not have permission to view backup/DR data.</div>
<% } else { %>

<div class="card">
  <h2 style="font-size:15px;margin-top:0">RPO/RTO Report</h2>
  <p style="color:var(--muted);font-size:12px">RTO target: <%= rtoTargetHours %>h (not independently measurable without an actual restore drill — see Restore Tests below).</p>
  <table>
    <thead><tr><th>Scope</th><th>Last Successful Backup</th><th>Actual RPO</th><th>Target RPO</th><th>Within Target</th></tr></thead>
    <tbody>
      <% for (const r of rpoReport) { %>
      <tr>
        <td><%= r.scope %></td>
        <td><%= r.lastSuccessfulBackupAt ? new Date(r.lastSuccessfulBackupAt).toLocaleString() : 'Never' %></td>
        <td><%= r.actualRpoHours != null ? r.actualRpoHours + 'h' : '—' %></td>
        <td><%= r.rpoTargetHours %>h</td>
        <td><span class="badge <%= r.rpoWithinTarget ? 'badge-ok' : 'badge-danger' %>"><%= r.rpoWithinTarget ? 'Yes' : 'No' %></span></td>
      </tr>
      <% } %>
      <% if (!rpoReport.length) { %><tr><td colspan="5" style="color:var(--muted)">No backups recorded yet for any scope.</td></tr><% } %>
    </tbody>
  </table>
</div>

<div class="card">
  <h2 style="font-size:15px;margin-top:0">Backup Records</h2>
  <p style="color:var(--muted);font-size:12px">Logging/reporting only — this environment has no automated backup pipeline to trigger; entries are recorded manually as backups actually run (e.g. by an ops script or scheduled job elsewhere).</p>
  <table>
    <thead><tr><th>Scope</th><th>Type</th><th>Status</th><th>Size</th><th>Started</th><th></th></tr></thead>
    <tbody>
      <% for (const b of backups) { %>
      <tr>
        <td><%= b.scope %></td>
        <td><%= b.backup_type %></td>
        <td><span class="badge <%= { succeeded: 'badge-ok', running: 'badge-warn', failed: 'badge-danger' }[b.status] %>"><%= b.status %></span></td>
        <td><%= b.size_bytes ? (b.size_bytes / 1e6).toFixed(1) + ' MB' : '—' %></td>
        <td><%= new Date(b.started_at).toLocaleString() %></td>
        <td><% if (b.status === 'succeeded') { %>
          <form method="post" action="/ops/backups/<%= b.id %>/restore-tests" onsubmit="return fillRestoreNotes(this)">
            <input type="hidden" name="status" value="success"><input type="hidden" name="notes">
            <button class="link-btn" type="submit">Log Restore Test</button>
          </form>
        <% } %></td>
      </tr>
      <% } %>
      <% if (!backups.length) { %><tr><td colspan="6" style="color:var(--muted)">No backups recorded.</td></tr><% } %>
    </tbody>
  </table>

  <form method="post" action="/ops/backups" style="display:flex;gap:6px;margin-top:12px;flex-wrap:wrap">
    <input type="text" name="scope" placeholder="e.g. lpms_dev" style="flex:1" required>
    <select name="backupType"><option value="full">Full</option><option value="incremental">Incremental</option></select>
    <select name="status"><option value="succeeded">Succeeded</option><option value="failed">Failed</option></select>
    <input type="text" name="location" placeholder="Storage location (opt.)" style="flex:1">
    <button class="btn" type="submit">Record Backup</button>
  </form>
</div>

<div class="card">
  <h2 style="font-size:15px;margin-top:0">Restore Tests</h2>
  <table>
    <thead><tr><th>Scope</th><th>Result</th><th>Tested By</th><th>When</th><th>Notes</th></tr></thead>
    <tbody>
      <% for (const t of restoreTests) { %>
      <tr>
        <td><%= t.scope %></td>
        <td><span class="badge <%= t.status === 'success' ? 'badge-ok' : 'badge-danger' %>"><%= t.status %></span></td>
        <td><%= t.tested_by %></td>
        <td><%= new Date(t.tested_at).toLocaleString() %></td>
        <td><%= t.notes || '' %></td>
      </tr>
      <% } %>
      <% if (!restoreTests.length) { %><tr><td colspan="5" style="color:var(--muted)">No restore tests logged yet.</td></tr><% } %>
    </tbody>
  </table>
</div>

<script>
function fillRestoreNotes(form) {
  const notes = prompt('Restore test notes (optional):') || '';
  form.notes.value = notes;
  return true;
}
</script>

<% } %>
