// ========== UNIFIED BACKUP/RESTORE v2.0 ==========
// Single file captures everything: server config + browser state + themes + encryption key
(function() {
  // All DashCaddy localStorage keys to include in backup
  var BROWSER_STATE_KEYS = [
    'dashcaddy_site_config', 'dashcaddy_onboarding', 'dashcaddy-encryption-key',
    'dashcaddy-setup', 'dashcaddy-config',
    'theme', 'user-themes', 'custom-theme',
    'custom-apps', 'custom-services', 'toolbar-sections',
    'weather-location', 'weather-zip', 'weather-geo', 'weather-unit',
    'clock-style', 'clock-chimes', 'clock-chime-volume'
  ];

  // Collect all DashCaddy browser state from localStorage
  function collectBrowserState() {
    var state = {};
    // Grab all whitelisted keys
    for (var i = 0; i < BROWSER_STATE_KEYS.length; i++) {
      var key = BROWSER_STATE_KEYS[i];
      var val = safeGet(key);
      if (val !== null && val !== undefined) state[key] = val;
    }
    // Grab dynamic widget-*-enabled keys
    try {
      for (var j = 0; j < localStorage.length; j++) {
        var k = localStorage.key(j);
        if (/^widget-.+-enabled$/.test(k)) {
          state[k] = localStorage.getItem(k);
        }
      }
    } catch (e) { /* private browsing */ }
    return state;
  }

  // Restore browser state from backup into localStorage
  function restoreBrowserState(browserState) {
    if (!browserState || typeof browserState !== 'object') return 0;
    var count = 0;
    for (var key in browserState) {
      if (!browserState.hasOwnProperty(key)) continue;
      safeSet(key, browserState[key]);
      count++;
    }
    return count;
  }

  // Handle legacy v1.0.0 import-export format (pre-backup modal)
  function isLegacyFormat(data) {
    return data.version && !data.files && data.services;
  }

  function restoreLegacyFormat(data) {
    // Map the old flat keys into browserState for localStorage restore
    var browserState = {};
    if (data.customServices) browserState['custom-services'] = JSON.stringify(data.customServices);
    if (data.customApps) browserState['custom-apps'] = JSON.stringify(data.customApps);
    if (data.weatherZip) browserState['weather-zip'] = data.weatherZip;
    if (data.theme) browserState['theme'] = data.theme;
    if (data.userThemes && Object.keys(data.userThemes).length) browserState['user-themes'] = JSON.stringify(data.userThemes);
    restoreBrowserState(browserState);

    // Push services to server if available
    if (data.services && Array.isArray(data.services)) {
      secureFetch('/api/v1/services', {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(data.services)
      }).catch(function() {});
    }

    // Push themes to server if available
    if (data.userThemes) {
      Object.keys(data.userThemes).forEach(function(slug) {
        var t = data.userThemes[slug];
        var colors = {};
        (window.THEME_PROPS || []).forEach(function(p) { if (t[p]) colors[p] = t[p]; });
        secureFetch('/api/v1/themes/' + slug, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ name: t.name || slug, colors: colors })
        }).catch(function() {});
      });
    }
  }

  // Inject modal HTML
  injectModal('backup-modal', `<div id="backup-modal" class="weather-modal">
    <div class="weather-modal-content" style="min-width: 550px; max-width: 650px;">
      <h3>💾 Backup & Restore</h3>
      <p class="modal-subtitle">
        Full backup of your entire DashCaddy setup — server config, credentials, themes, and browser preferences in one file.
      </p>

      <!-- Tab bar -->
      <div class="panel-tabs">
        <button class="panel-tab active" data-panel="backup-manual">Manual</button>
        <button class="panel-tab" data-panel="backup-automated">Automated</button>
        <button class="panel-tab" data-panel="backup-history-tab">History</button>
      </div>

      <!-- Tab: Manual -->
      <div id="backup-manual" class="panel-section active">
        <!-- Export Section -->
        <div style="margin-bottom: 20px; padding: 16px; background: linear-gradient(135deg, color-mix(in srgb, #27ae60 15%, transparent), color-mix(in srgb, #2ecc71 10%, transparent)); border-radius: 10px; border: 1px solid #27ae60;">
          <h4 style="margin: 0 0 8px; color: #2ecc71; font-size: 0.95rem;">📤 Export Backup</h4>
          <p style="font-size: 0.8rem; color: var(--muted); margin: 0 0 12px;">
            Downloads everything — services, Caddyfile, credentials, encryption key, themes, and all browser preferences.
          </p>
          <button id="backup-export-btn" style="width: 100%; padding: 10px; background: linear-gradient(135deg, #27ae60 0%, #2ecc71 100%); border: none; color: white; font-weight: 600; border-radius: 8px; cursor: pointer;">
            ⬇️ Download Full Backup
          </button>
        </div>

        <!-- Import Section -->
        <div style="padding: 16px; background: linear-gradient(135deg, color-mix(in srgb, #3498db 15%, transparent), color-mix(in srgb, #2980b9 10%, transparent)); border-radius: 10px; border: 1px solid #3498db;">
          <h4 style="margin: 0 0 8px; color: #3498db; font-size: 0.95rem;">📥 Restore Backup</h4>
          <p style="font-size: 0.8rem; color: var(--muted); margin: 0 0 12px;">
            Upload a backup file to restore your entire configuration — drag and drop ready.
          </p>
          <input type="file" id="backup-file-input" accept=".json" style="display: none;" />
          <button id="backup-select-file" style="width: 100%; padding: 10px; background: linear-gradient(135deg, #3498db 0%, #2980b9 100%); border: none; color: white; font-weight: 600; border-radius: 8px; cursor: pointer;">
            📁 Select Backup File
          </button>
          <div id="backup-file-name" style="display: none; margin-top: 8px; padding: 8px; background: var(--card-base); border-radius: 6px; font-size: 0.85rem;"></div>
        </div>

        <!-- Preview Section (shown after file selected) -->
        <div id="backup-preview" style="display: none; margin-top: 16px; padding: 16px; background: var(--card-base); border-radius: 10px; border: 1px solid var(--border);">
          <h4 style="margin: 0 0 12px; font-size: 0.9rem;">📋 Backup Contents</h4>
          <div id="backup-preview-content" style="font-size: 0.85rem;"></div>
          <div style="margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border);">
            <label class="checkbox-label" style="font-size: 0.85rem;">
              <input type="checkbox" id="backup-reload-caddy" checked />
              Reload Caddy after restore
            </label>
          </div>
          <button id="backup-do-restore-btn" style="width: 100%; margin-top: 12px; padding: 10px; background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%); border: none; color: white; font-weight: 600; border-radius: 8px; cursor: pointer;">
            ⚡ Restore Everything
          </button>
        </div>

        <!-- Result Message -->
        <div id="backup-result" style="display: none; margin-top: 16px; padding: 12px; border-radius: 8px;"></div>
      </div>

      <!-- Tab: Automated Backups -->
      <div id="backup-automated" class="panel-section">
        <div id="backup-schedule-container">
          <div class="panel-empty">
            <span class="empty-icon">⏰</span>
            <span class="brand-spinner"></span> Loading backup schedule...
          </div>
        </div>
      </div>

      <!-- Tab: Backup History -->
      <div id="backup-history-tab" class="panel-section">
        <div id="backup-history-container" style="max-height: 400px; overflow-y: auto;">
          <div class="panel-empty">
            <span class="empty-icon">📋</span>
            <span class="brand-spinner"></span> Loading backup history...
          </div>
        </div>
      </div>

      <!-- Close Button -->
      <div class="weather-modal-buttons modal-footer-bar">
        <button id="backup-cancel">Close</button>
      </div>
    </div>
  </div>`);

  var modal = document.getElementById('backup-modal');
  var openBtn = document.getElementById('backup-restore-btn');
  var cancelBtn = document.getElementById('backup-cancel');
  var exportBtn = document.getElementById('backup-export-btn');
  var selectFileBtn = document.getElementById('backup-select-file');
  var fileInput = document.getElementById('backup-file-input');
  var fileNameDiv = document.getElementById('backup-file-name');
  var previewDiv = document.getElementById('backup-preview');
  var previewContent = document.getElementById('backup-preview-content');
  var restoreBtn = document.getElementById('backup-do-restore-btn');
  var resultDiv = document.getElementById('backup-result');
  var scheduleContainer = document.getElementById('backup-schedule-container');
  var historyContainer = document.getElementById('backup-history-container');

  var selectedBackup = null;

  // Open modal
  openBtn?.addEventListener('click', function() {
    modal.classList.add('show');
    if (resultDiv) resultDiv.style.display = 'none';
    if (previewDiv) previewDiv.style.display = 'none';
    if (fileNameDiv) fileNameDiv.style.display = 'none';
    selectedBackup = null;
  });

  wireModal(modal, cancelBtn);

  // === EXPORT: Server backup + browser state in one file ===
  exportBtn?.addEventListener('click', async function() {
    exportBtn.disabled = true;
    exportBtn.innerHTML = '<span class="brand-spinner"></span> Exporting...';
    try {
      // Fetch server-side backup (config, services, caddyfile, credentials, encryption key, themes, etc.)
      var response = await fetch('/api/v1/backup/export');
      var data = await response.json();

      // Add all browser localStorage state
      data.browserState = collectBrowserState();

      // Download unified backup
      var blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
      var url = URL.createObjectURL(blob);
      var a = document.createElement('a');
      a.href = url;
      a.download = 'dashcaddy-backup-' + new Date().toISOString().split('T')[0] + '.json';
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      URL.revokeObjectURL(url);

      var stateCount = Object.keys(data.browserState).length;
      var themeCount = data.themes ? Object.keys(data.themes).length : 0;
      resultDiv.innerHTML = '✅ Full backup downloaded — server config + ' + stateCount + ' browser settings' + (themeCount ? ' + ' + themeCount + ' themes' : '');
      resultDiv.style.display = 'block';
      resultDiv.style.background = 'color-mix(in srgb, var(--ok-fg) 15%, transparent)';
      resultDiv.style.border = '1px solid var(--ok-fg)';
    } catch (e) {
      resultDiv.innerHTML = '❌ Export failed: ' + escapeHtml(e.message);
      resultDiv.style.display = 'block';
      resultDiv.style.background = 'color-mix(in srgb, var(--bad-fg) 15%, transparent)';
      resultDiv.style.border = '1px solid var(--bad-fg)';
    }
    exportBtn.disabled = false;
    exportBtn.innerHTML = '⬇️ Download Full Backup';
  });

  // Select file button
  selectFileBtn?.addEventListener('click', function() { fileInput.click(); });

  // === FILE SELECTED: Preview contents ===
  fileInput?.addEventListener('change', async function(e) {
    var file = e.target.files[0];
    if (!file) return;
    fileNameDiv.textContent = '📄 ' + file.name;
    fileNameDiv.style.display = 'block';
    resultDiv.style.display = 'none';
    try {
      var text = await file.text();
      var backup = JSON.parse(text);

      // Handle legacy v1.0.0 format (from old import-export.js)
      if (isLegacyFormat(backup)) {
        selectedBackup = backup;
        var html = '<div style="margin-bottom: 8px; color: var(--muted); font-size: 0.8rem;">Legacy format (v' + escapeHtml(backup.version) + ')</div>';
        html += '<div style="display: flex; flex-wrap: wrap; gap: 6px;">';
        if (backup.services?.length) html += '<span style="padding: 4px 8px; background: var(--base); border-radius: 4px; font-size: 0.8rem;">📋 ' + backup.services.length + ' services</span>';
        if (backup.customApps?.length) html += '<span style="padding: 4px 8px; background: var(--base); border-radius: 4px; font-size: 0.8rem;">📦 ' + backup.customApps.length + ' custom apps</span>';
        if (backup.theme) html += '<span style="padding: 4px 8px; background: var(--base); border-radius: 4px; font-size: 0.8rem;">🎨 Theme: ' + escapeHtml(backup.theme) + '</span>';
        if (backup.userThemes) html += '<span style="padding: 4px 8px; background: var(--base); border-radius: 4px; font-size: 0.8rem;">🎨 ' + Object.keys(backup.userThemes).length + ' custom themes</span>';
        html += '</div>';
        previewContent.innerHTML = html;
        previewDiv.style.display = 'block';
        return;
      }

      // v1.1+ / v2.0 format — send to server for preview
      var response = await secureFetch('/api/v1/backup/preview', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(backup)
      });
      var preview = await response.json();
      if (preview.success) {
        selectedBackup = backup;
        var html = '<div style="margin-bottom: 8px; color: var(--muted); font-size: 0.8rem;">Exported: ' + new Date(backup.exportedAt).toLocaleString() + ' (v' + escapeHtml(backup.version) + ')</div>';

        // Server files
        html += '<div style="margin-bottom: 6px; font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px;">Server Config</div>';
        html += '<div style="display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px;">';
        for (var key in preview.preview.files) {
          var info = preview.preview.files[key];
          var icon = info.action === 'create' ? '🆕' : '📝';
          html += '<span style="padding: 4px 8px; background: var(--base); border-radius: 4px; font-size: 0.8rem;">' + icon + ' ' + escapeHtml(info.description) + '</span>';
        }
        html += '</div>';

        // Services count
        if (preview.preview.serviceCount) {
          html += '<div style="font-size: 0.8rem; color: var(--accent); margin-bottom: 6px;">' + preview.preview.serviceCount + ' services</div>';
        }

        // Themes
        if (preview.preview.themeCount) {
          html += '<div style="font-size: 0.8rem; color: var(--accent); margin-bottom: 6px;">🎨 ' + preview.preview.themeCount + ' custom themes</div>';
        }

        // Browser state
        if (preview.preview.browserStateCount) {
          html += '<div style="margin-top: 6px; font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px;">Browser Preferences</div>';
          html += '<div style="font-size: 0.8rem; color: var(--accent);">🖥️ ' + preview.preview.browserStateCount + ' saved settings (theme, weather, clock, widgets, etc.)</div>';
        }

        previewContent.innerHTML = html;
        previewDiv.style.display = 'block';
      } else {
        resultDiv.innerHTML = '⚠️ Invalid backup file: ' + escapeHtml(preview.error);
        resultDiv.style.display = 'block';
        resultDiv.style.background = 'color-mix(in srgb, #f39c12 15%, transparent)';
        resultDiv.style.border = '1px solid #f39c12';
        previewDiv.style.display = 'none';
      }
    } catch (e) {
      resultDiv.innerHTML = '❌ Could not read file: ' + escapeHtml(e.message);
      resultDiv.style.display = 'block';
      resultDiv.style.background = 'color-mix(in srgb, var(--bad-fg) 15%, transparent)';
      resultDiv.style.border = '1px solid var(--bad-fg)';
      previewDiv.style.display = 'none';
    }
  });

  // === RESTORE: Server + browser state ===
  restoreBtn?.addEventListener('click', async function() {
    if (!selectedBackup) return;
    if (!confirm('This will overwrite your current configuration and browser preferences. Continue?')) return;
    restoreBtn.disabled = true;
    restoreBtn.innerHTML = '<span class="brand-spinner"></span> Restoring...';
    try {
      // Handle legacy format
      if (isLegacyFormat(selectedBackup)) {
        restoreLegacyFormat(selectedBackup);
        resultDiv.innerHTML = '✅ Legacy backup restored — browser settings and services imported.';
        resultDiv.style.background = 'color-mix(in srgb, var(--ok-fg) 15%, transparent)';
        resultDiv.style.border = '1px solid var(--ok-fg)';
        resultDiv.style.display = 'block';
        setTimeout(function() { location.reload(); }, 2000);
        restoreBtn.disabled = false;
        restoreBtn.innerHTML = '⚡ Restore Everything';
        return;
      }

      // v1.1+ / v2.0 — restore server-side first
      var reloadCaddy = document.getElementById('backup-reload-caddy')?.checked ?? true;
      var response = await secureFetch('/api/v1/backup/restore', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ backup: selectedBackup, options: { reloadCaddy: reloadCaddy } })
      });
      var data = await response.json();

      // Then restore browser state
      var browserCount = 0;
      if (selectedBackup.browserState) {
        browserCount = restoreBrowserState(selectedBackup.browserState);
      }

      if (data.success) {
        var msg = '✅ ' + data.message;
        if (browserCount > 0) msg += '<br><small style="color: var(--muted);">' + browserCount + ' browser settings restored</small>';
        if (data.results.caddyReloaded) msg += '<br><small style="color: var(--muted);">Caddy configuration reloaded</small>';
        resultDiv.innerHTML = msg;
        resultDiv.style.background = 'color-mix(in srgb, var(--ok-fg) 15%, transparent)';
        resultDiv.style.border = '1px solid var(--ok-fg)';
        setTimeout(function() { location.reload(); }, 2000);
      } else {
        resultDiv.innerHTML = '⚠️ ' + escapeHtml(data.message);
        if (browserCount > 0) resultDiv.innerHTML += '<br><small style="color: var(--muted);">' + browserCount + ' browser settings were restored</small>';
        if (data.results?.errors?.length > 0) {
          resultDiv.innerHTML += '<br><small>' + data.results.errors.map(function(e) { return escapeHtml(e.file) + ': ' + escapeHtml(e.error); }).join(', ') + '</small>';
        }
        resultDiv.style.background = 'color-mix(in srgb, #f39c12 15%, transparent)';
        resultDiv.style.border = '1px solid #f39c12';
      }
      resultDiv.style.display = 'block';
    } catch (e) {
      resultDiv.innerHTML = '❌ Restore failed: ' + escapeHtml(e.message);
      resultDiv.style.display = 'block';
      resultDiv.style.background = 'color-mix(in srgb, var(--bad-fg) 15%, transparent)';
      resultDiv.style.border = '1px solid var(--bad-fg)';
    }
    restoreBtn.disabled = false;
    restoreBtn.innerHTML = '⚡ Restore Everything';
  });

  // === Automated Backups Tab ===
  async function loadBackupSchedule() {
    if (!scheduleContainer) return;
    try {
      var res = await fetch('/api/v1/backups/config');
      var data = await res.json();
      if (!data.success) throw new Error(data.error || 'Failed to load config');
      var cfg = data.config?.backups || {};
      var autoKey = Object.keys(cfg)[0];
      var auto = autoKey ? cfg[autoKey] : null;

      var html = '<div style="padding: 16px; background: color-mix(in srgb, var(--accent) 8%, transparent); border-radius: 10px; border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent); margin-bottom: 16px;">';
      html += '<h4 style="margin: 0 0 12px; color: var(--accent); font-size: 0.9rem;">⏰ Backup Schedule</h4>';
      html += '<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 12px;">';
      html += '<div><label style="font-size: 0.8rem; color: var(--muted);">Schedule:</label>';
      html += '  <select id="backup-schedule-select" style="width: 100%;">';
      html += '    <option value="disabled"' + (!auto?.enabled ? ' selected' : '') + '>Disabled</option>';
      html += '    <option value="hourly"' + (auto?.schedule === 'hourly' ? ' selected' : '') + '>Hourly</option>';
      html += '    <option value="daily"' + (auto?.schedule === 'daily' ? ' selected' : '') + '>Daily</option>';
      html += '    <option value="weekly"' + (auto?.schedule === 'weekly' ? ' selected' : '') + '>Weekly</option>';
      html += '    <option value="monthly"' + (auto?.schedule === 'monthly' ? ' selected' : '') + '>Monthly</option>';
      html += '  </select></div>';
      html += '<div><label style="font-size: 0.8rem; color: var(--muted);">Keep last:</label>';
      html += '  <select id="backup-retention-select" style="width: 100%;">';
      html += '    <option value="3"' + (auto?.retention?.keep === 3 ? ' selected' : '') + '>3 backups</option>';
      html += '    <option value="5"' + (!auto?.retention || auto?.retention?.keep === 5 ? ' selected' : '') + '>5 backups</option>';
      html += '    <option value="10"' + (auto?.retention?.keep === 10 ? ' selected' : '') + '>10 backups</option>';
      html += '    <option value="30"' + (auto?.retention?.keep === 30 ? ' selected' : '') + '>30 backups</option>';
      html += '  </select></div>';
      html += '</div>';
      html += '<div style="margin-top: 12px;">';
      html += '  <label style="display: flex; align-items: center; gap: 8px; font-size: 0.85rem; cursor: pointer;">';
      html += '    <input type="checkbox" id="backup-encrypt-toggle"' + (auto?.encrypt !== false ? ' checked' : '') + ' />';
      html += '    Encrypt backups';
      html += '  </label></div>';
      html += '<div style="display: flex; gap: 8px; margin-top: 12px;">';
      html += '  <button id="backup-save-schedule" style="padding: 8px 16px; background: color-mix(in srgb, var(--accent) 20%, transparent); border: 1px solid var(--accent); color: var(--accent); border-radius: 6px; cursor: pointer; font-weight: 500;">Save Schedule</button>';
      html += '  <button id="backup-run-now" style="padding: 8px 16px; border-radius: 6px; cursor: pointer;">▶️ Run Backup Now</button>';
      html += '</div>';
      html += '</div>';
      html += '<div id="backup-schedule-result" style="display: none; margin-top: 12px; padding: 10px; border-radius: 8px; font-size: 0.85rem;"></div>';
      scheduleContainer.innerHTML = html;

      document.getElementById('backup-save-schedule')?.addEventListener('click', saveSchedule);
      document.getElementById('backup-run-now')?.addEventListener('click', runBackupNow);
    } catch (e) {
      scheduleContainer.innerHTML = '<div class="panel-empty" style="color: var(--bad-fg);">Failed to load schedule: ' + escapeHtml(e.message) + '</div>';
    }
  }

  async function saveSchedule() {
    var schedule = document.getElementById('backup-schedule-select')?.value;
    var retention = parseInt(document.getElementById('backup-retention-select')?.value) || 5;
    var encrypt = document.getElementById('backup-encrypt-toggle')?.checked ?? true;
    var resultEl = document.getElementById('backup-schedule-result');
    try {
      var res = await secureFetch('/api/v1/backups/config', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          backups: {
            auto: {
              enabled: schedule !== 'disabled',
              schedule: schedule === 'disabled' ? 'daily' : schedule,
              include: ['all'],
              encrypt: encrypt,
              verify: true,
              retention: { keep: retention },
              destinations: [{ type: 'local' }]
            }
          }
        })
      });
      var data = await res.json();
      if (resultEl) {
        resultEl.innerHTML = data.success ? '✅ Schedule saved' : '⚠️ ' + escapeHtml(data.error);
        resultEl.style.display = 'block';
        resultEl.style.background = data.success ? 'color-mix(in srgb, var(--ok-fg) 15%, transparent)' : 'color-mix(in srgb, var(--bad-fg) 15%, transparent)';
        resultEl.style.border = data.success ? '1px solid var(--ok-fg)' : '1px solid var(--bad-fg)';
        setTimeout(function() { if (resultEl) resultEl.style.display = 'none'; }, 3000);
      }
    } catch (e) {
      if (resultEl) {
        resultEl.innerHTML = '❌ ' + escapeHtml(e.message);
        resultEl.style.display = 'block';
        resultEl.style.background = 'color-mix(in srgb, var(--bad-fg) 15%, transparent)';
        resultEl.style.border = '1px solid var(--bad-fg)';
      }
    }
  }

  async function runBackupNow() {
    var btn = document.getElementById('backup-run-now');
    var resultEl = document.getElementById('backup-schedule-result');
    if (btn) { btn.disabled = true; btn.innerHTML = '<span class="brand-spinner"></span> Running...'; }
    try {
      var res = await secureFetch('/api/v1/backups/execute', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ include: ['all'], destinations: [{ type: 'local' }] })
      });
      var data = await res.json();
      if (resultEl) {
        if (data.success) {
          var sizeMB = data.backup?.size ? (data.backup.size / 1024 / 1024).toFixed(2) : '?';
          resultEl.innerHTML = '✅ Backup complete (' + sizeMB + ' MB)';
          resultEl.style.background = 'color-mix(in srgb, var(--ok-fg) 15%, transparent)';
          resultEl.style.border = '1px solid var(--ok-fg)';
        } else {
          resultEl.innerHTML = '⚠️ ' + escapeHtml(data.error);
          resultEl.style.background = 'color-mix(in srgb, var(--bad-fg) 15%, transparent)';
          resultEl.style.border = '1px solid var(--bad-fg)';
        }
        resultEl.style.display = 'block';
      }
      loadBackupHistory();
    } catch (e) {
      if (resultEl) {
        resultEl.innerHTML = '❌ ' + escapeHtml(e.message);
        resultEl.style.display = 'block';
        resultEl.style.background = 'color-mix(in srgb, var(--bad-fg) 15%, transparent)';
        resultEl.style.border = '1px solid var(--bad-fg)';
      }
    }
    if (btn) { btn.disabled = false; btn.innerHTML = '▶️ Run Backup Now'; }
  }

  // === Backup History Tab ===
  async function loadBackupHistory() {
    if (!historyContainer) return;
    historyContainer.innerHTML = '<div class="panel-empty"><span class="brand-spinner"></span> Loading...</div>';
    try {
      var res = await fetch('/api/v1/backups/history?limit=50');
      var data = await res.json();
      if (!data.success || !data.history?.length) {
        historyContainer.innerHTML = '<div class="panel-empty"><span class="empty-icon">📋</span> No backup history yet</div>';
        return;
      }
      var html = '<div style="display: flex; flex-direction: column; gap: 6px;">';
      for (var i = 0; i < data.history.length; i++) {
        var bk = data.history[i];
        var sizeMB = bk.size ? (bk.size / 1024 / 1024).toFixed(2) : '?';
        html += '<div style="padding: 10px 12px; background: var(--card-base); border-radius: 6px; border: 1px solid var(--border); font-size: 0.85rem;">';
        html += '  <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px;">';
        html += '    <span style="font-weight: 500;">' + escapeHtml(bk.name || 'backup') + '</span>';
        html += '    <div style="display: flex; align-items: center; gap: 8px;">';
        html += '      <span class="status-badge ' + (bk.status === 'success' ? 'success' : 'down') + '">' + escapeHtml(bk.status) + '</span>';
        if (bk.status === 'success') html += '      <button class="backup-restore-btn" data-backup-id="' + escapeHtml(bk.id) + '" style="padding: 3px 8px; font-size: 0.75rem;">Restore</button>';
        html += '    </div>';
        html += '  </div>';
        html += '  <div style="font-size: 0.75rem; color: var(--muted);">';
        html += '    ' + new Date(bk.timestamp).toLocaleString() + ' | ' + sizeMB + ' MB | ' + (bk.duration ? (bk.duration / 1000).toFixed(1) + 's' : '--');
        if (bk.encrypted) html += ' | 🔒';
        html += '  </div>';
        html += '</div>';
      }
      html += '</div>';
      historyContainer.innerHTML = html;
      historyContainer.querySelectorAll('.backup-restore-btn').forEach(function(btn) {
        btn.addEventListener('click', function() { window.__restoreServerBackup(btn.dataset.backupId); });
      });
    } catch (e) {
      historyContainer.innerHTML = '<div class="panel-empty" style="color: var(--bad-fg);">Failed: ' + escapeHtml(e.message) + '</div>';
    }
  }

  window.__restoreServerBackup = async function(backupId) {
    if (!confirm('Restore from this server backup? This will overwrite current configuration.')) return;
    try {
      var res = await secureFetch('/api/v1/backups/restore/' + backupId, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ restoreServices: true, restoreConfig: true })
      });
      var data = await res.json();
      if (data.success) {
        showNotification('Restore completed successfully!', 'success');
        location.reload();
      } else {
        showNotification('Restore failed: ' + (data.error || 'Unknown error'), 'error');
      }
    } catch (e) { showNotification('Restore error: ' + e.message, 'error'); }
  };

  // Lazy-load tabs
  document.querySelector('[data-panel="backup-automated"]')?.addEventListener('click', loadBackupSchedule);
  document.querySelector('[data-panel="backup-history-tab"]')?.addEventListener('click', loadBackupHistory);
})();
