import { initI18n, t } from "./i18n";
import { initSidebar } from "./sidebar";

// admin-rules-export.ts — /admin/rules/export. Calls
// GET /admin/api/rules/export-migrations[?since=<uuid>] and renders the
// SQL blob server-side. Download builds a Blob URL and triggers a
// fake <a> click; copy uses navigator.clipboard.

interface ExportResult {
  migration_sql: string;
  count: number;
  latest_audit_id: string;
}

let latest: ExportResult | null = null;

function showFeedback(msg: string, isError: boolean) {
  const el = document.getElementById("export-feedback") as HTMLElement | null;
  if (!el) return;
  el.textContent = msg;
  el.className = "form-msg " + (isError ? "form-msg-error" : "form-msg-success");
  el.style.display = "block";
  if (!isError) setTimeout(() => { el.style.display = "none"; }, 4000);
}

async function runExport() {
  const since = (document.getElementById("export-since") as HTMLInputElement).value.trim();
  const qs = new URLSearchParams();
  if (since) qs.set("since", since);
  const url = "/admin/api/rules/export-migrations" + (qs.toString() ? "?" + qs.toString() : "");
  const out = document.getElementById("export-output") as HTMLElement;
  const summary = document.getElementById("export-summary") as HTMLElement;
  const dl = document.getElementById("export-download") as HTMLElement;
  const cp = document.getElementById("export-copy") as HTMLElement;
  out.textContent = t("admin.rules.export.running") || "Lade...";
  summary.style.display = "none";
  dl.style.display = "none";
  cp.style.display = "none";

  const resp = await fetch(url);
  if (!resp.ok) {
    const body = await resp.json().catch(() => ({ error: resp.statusText }));
    showFeedback(body.error || (t("admin.rules.export.error") || "Export fehlgeschlagen."), true);
    out.textContent = "";
    return;
  }
  latest = await resp.json() as ExportResult;
  out.textContent = latest.migration_sql;
  summary.style.display = "";
  const countEl = document.getElementById("export-summary-count") as HTMLElement;
  const latestEl = document.getElementById("export-summary-latest") as HTMLElement;
  countEl.textContent = (t("admin.rules.export.count") || "Audit-Zeilen: {n}").replace("{n}", String(latest.count));
  if (latest.latest_audit_id) {
    latestEl.textContent = (t("admin.rules.export.latest") || "Letzte Audit-ID: {id}").replace("{id}", latest.latest_audit_id);
  } else {
    latestEl.textContent = "";
  }
  if (latest.count > 0) {
    dl.style.display = "";
    cp.style.display = "";
    showFeedback((t("admin.rules.export.ok") || "{n} Audit-Zeilen exportiert.").replace("{n}", String(latest.count)), false);
  } else {
    showFeedback(t("admin.rules.export.no_pending") || "Keine offenen Audit-Zeilen zum Export.", false);
  }
}

function downloadFile() {
  if (!latest) return;
  const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
  const name = `rules-export-${ts}.up.sql`;
  const blob = new Blob([latest.migration_sql], { type: "application/sql" });
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = name;
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  URL.revokeObjectURL(url);
}

async function copyToClipboard() {
  if (!latest) return;
  try {
    await navigator.clipboard.writeText(latest.migration_sql);
    showFeedback(t("admin.rules.export.copied") || "In Zwischenablage kopiert.", false);
  } catch (e) {
    showFeedback(t("admin.rules.export.copy_failed") || "Kopieren fehlgeschlagen.", true);
  }
}

function init() {
  initI18n();
  initSidebar();
  (document.getElementById("export-run") as HTMLElement).addEventListener("click", runExport);
  (document.getElementById("export-download") as HTMLElement).addEventListener("click", downloadFile);
  (document.getElementById("export-copy") as HTMLElement).addEventListener("click", copyToClipboard);
}

document.addEventListener("DOMContentLoaded", init);
