"use strict";

const requestMonitoringToggle = document.getElementById(
  "request-monitoring-toggle"
);
const requestMonitoringStatus = document.getElementById(
  "request-monitoring-status"
);
const consentCaptureToggle = document.getElementById("consent-capture-toggle");
const consentCaptureStatus = document.getElementById("consent-capture-status");
const maintenanceWarning = document.getElementById("maintenance-warning");
const evidenceLockedCount = document.getElementById("evidence-locked-count");
const evidenceDashboardButton = document.getElementById(
  "evidence-dashboard-button"
);
const evidenceExportJsonButton = document.getElementById(
  "evidence-export-json-button"
);
const evidencePurgeUnlockedButton = document.getElementById(
  "evidence-purge-unlocked-button"
);
const evidencePurgeConfirmModal = document.getElementById(
  "evidence-purge-confirm-modal"
);
const evidencePurgeCancelButton = document.getElementById(
  "evidence-purge-cancel-button"
);
const evidencePurgeConfirmButton = document.getElementById(
  "evidence-purge-confirm-button"
);
const evidenceExportJsonStatus = document.getElementById(
  "evidence-export-json-status"
);
const evidenceRetentionStatus = document.getElementById(
  "evidence-retention-status"
);

const evidenceStoreCountCells = {
  consent_states: document.getElementById("evidence-consent-states-count"),
  consent_events: document.getElementById("evidence-consent-events-count"),
  observed_requests: document.getElementById("evidence-observed-requests-count"),
  gvl_snapshots: document.getElementById("evidence-gvl-snapshots-count"),
  gvl_snapshot_events: document.getElementById(
    "evidence-gvl-snapshot-events-count"
  )
};

document.addEventListener("DOMContentLoaded", async () => {
  await renderSettings();
  await renderEvidenceMaintenanceStatus();
  await renderEvidenceRetentionStatus();

  consentCaptureToggle.addEventListener("change", async () => {
    consentCaptureToggle.disabled = true;

    await setVendorGetSetting(
      "consentCaptureEnabled",
      consentCaptureToggle.checked
    );
    await renderSettings();

    consentCaptureToggle.disabled = false;
  });

  requestMonitoringToggle.addEventListener("change", async () => {
    requestMonitoringToggle.disabled = true;

    await setVendorGetSetting(
      "requestMonitoringEnabled",
      requestMonitoringToggle.checked
    );
    await renderSettings();

    requestMonitoringToggle.disabled = false;
  });

  evidenceDashboardButton.addEventListener("click", async () => {
    await browser.tabs.create({
      url: browser.runtime.getURL("src/dashboard/dashboard.html")
    });
  });

  evidenceExportJsonButton.addEventListener("click", exportEvidenceJsonFile);
  evidencePurgeUnlockedButton.addEventListener("click", openPurgeConfirmModal);
  evidencePurgeCancelButton.addEventListener("click", closePurgeConfirmModal);
  evidencePurgeConfirmButton.addEventListener("click", purgeUnlockedEvidence);
});

async function renderSettings() {
  const settings = await getVendorGetSettings();
  const consentCaptureEnabled = settings.consentCaptureEnabled;
  const requestMonitoringEnabled = settings.requestMonitoringEnabled;

  consentCaptureToggle.checked = consentCaptureEnabled;
  consentCaptureStatus.textContent = consentCaptureEnabled ? "aktiv" : "aus";

  requestMonitoringToggle.checked = requestMonitoringEnabled;
  requestMonitoringStatus.textContent = requestMonitoringEnabled
    ? "aktiv"
    : "aus";
}

async function renderEvidenceRetentionStatus() {
  try {
    const status = await getEvidenceRetentionStatus();

    renderEvidenceStoreCounts(status.storeCounts ?? {});
    evidenceLockedCount.textContent = formatStatusValue(status.lockedCount);
    renderEvidenceRetentionMessage("Bereit");
  } catch (error) {
    renderEvidenceStoreCounts({});
    evidenceLockedCount.textContent = "-";
    renderEvidenceRetentionMessage("Status konnte nicht geladen werden");
    console.warn("VendorGet-IV evidence retention status failed", error);
  }
}

async function renderEvidenceMaintenanceStatus() {
  try {
    const status = await browser.runtime.sendMessage({
      type: "get_evidence_maintenance_status"
    });

    if (!status?.success) {
      throw new Error(status?.error ?? "get_evidence_maintenance_status_failed");
    }

    maintenanceWarning.hidden = !status.evidenceWriteSuspended;
  } catch (error) {
    maintenanceWarning.hidden = true;
    console.warn("VendorGet-IV maintenance status failed", error);
  }
}

async function getEvidenceRetentionStatus() {
  const status = await browser.runtime.sendMessage({
    type: "get_evidence_retention_status"
  });

  if (!status?.success) {
    throw new Error(status?.error ?? "get_evidence_retention_status_failed");
  }

  return status;
}

function renderEvidenceStoreCounts(storeCounts) {
  for (const [storeName, cell] of Object.entries(evidenceStoreCountCells)) {
    cell.textContent = formatStatusValue(storeCounts[storeName]);
  }
}

function formatStatusValue(value) {
  return Number.isFinite(value) ? String(value) : "-";
}

function renderEvidenceRetentionMessage(message) {
  evidenceRetentionStatus.textContent = message;
}

function openPurgeConfirmModal() {
  evidencePurgeConfirmModal.hidden = false;
  evidencePurgeCancelButton.focus();
}

function closePurgeConfirmModal() {
  evidencePurgeConfirmModal.hidden = true;
  evidencePurgeUnlockedButton.focus();
}

async function purgeUnlockedEvidence() {
  closePurgeConfirmModal();
  evidencePurgeUnlockedButton.disabled = true;
  renderEvidenceRetentionMessage("Ungesperrte Evidence-Daten werden gelöscht...");

  try {
    const result = await browser.runtime.sendMessage({
      type: "purge_unlocked_evidence_records"
    });

    if (!result?.success) {
      throw new Error(result?.error ?? "purge_unlocked_evidence_records_failed");
    }

    await renderEvidenceRetentionStatus();
    renderEvidenceRetentionMessage(buildPurgeUnlockedSuccessMessage(result));
  } catch (error) {
    renderEvidenceRetentionMessage("Löschen fehlgeschlagen");
    console.warn("VendorGet-IV unlocked evidence purge failed", error);
  } finally {
    evidencePurgeUnlockedButton.disabled = false;
  }
}

function buildPurgeUnlockedSuccessMessage(result) {
  if (Number.isFinite(result.deletedCount)) {
    return `Ungesperrte Evidence-Daten gelöscht: ${result.deletedCount} Records`;
  }

  return "Ungesperrte Evidence-Daten gelöscht";
}

async function exportEvidenceJsonFile() {
  evidenceExportJsonButton.disabled = true;
  renderEvidenceExportJsonMessage("Export läuft…");

  try {
    const result = await browser.runtime.sendMessage({
      type: "export_evidence_json"
    });

    if (!result?.success || !result.export) {
      throw new Error(result?.error ?? "export_evidence_json_failed");
    }

    downloadJsonExport(result.export);
    renderEvidenceExportJsonMessage(buildExportSuccessMessage(result.export));
  } catch (error) {
    renderEvidenceExportJsonMessage("Export fehlgeschlagen");
    console.warn("VendorGet-IV evidence JSON export failed", error);
  } finally {
    evidenceExportJsonButton.disabled = false;
  }
}

function downloadJsonExport(exportContainer) {
  const json = JSON.stringify(exportContainer, null, 2);
  const blob = new Blob([json], { type: "application/json" });
  const url = URL.createObjectURL(blob);
  const downloadLink = document.createElement("a");

  downloadLink.href = url;
  downloadLink.download = `vendorget-evidence-export-${formatExportTimestamp(
    new Date()
  )}.json`;
  document.body.append(downloadLink);
  downloadLink.click();
  downloadLink.remove();

  setTimeout(() => URL.revokeObjectURL(url), 0);
}

function buildExportSuccessMessage(exportContainer) {
  const storeCount = exportContainer?.metadata?.stores?.length ?? 0;
  const totalRecordCount =
    exportContainer?.metadata?.summary?.totalRecordCount ?? 0;

  return `Export gespeichert: ${storeCount} Stores, ${totalRecordCount} Records`;
}

function formatExportTimestamp(date) {
  const year = date.getFullYear();
  const month = padDatePart(date.getMonth() + 1);
  const day = padDatePart(date.getDate());
  const hours = padDatePart(date.getHours());
  const minutes = padDatePart(date.getMinutes());
  const seconds = padDatePart(date.getSeconds());

  return `${year}${month}${day}-${hours}${minutes}${seconds}`;
}

function padDatePart(value) {
  return String(value).padStart(2, "0");
}

function renderEvidenceExportJsonMessage(message) {
  evidenceExportJsonStatus.textContent = message;
}
