/* global window */
const OPS_ENDPOINTS = {
  summary: "/api/ops/summary",
  queue: "/api/ops/queue",
  customers: "/api/ops/customers",
  productHealth: "/api/ops/product-health",
  engineering: "/api/ops/engineering",
  marketing: "/api/ops/marketing",
  documents: "/api/ops/documents",
  grants: "/api/ops/grants",
  investors: "/api/ops/investors",
  finance: "/api/ops/finance",
  legal: "/api/ops/legal",
  automations: "/api/ops/automations",
  settings: "/api/ops/settings",
};

async function loadOpsSection(sectionName) {
  const path = OPS_ENDPOINTS[sectionName];
  if (!path) throw new Error(`Unknown ops section: ${sectionName}`);
  const payload = await window.OpsApi.opsFetch(path);
  return window.OpsProvenance.assertRuntimeProvenance(sectionName, payload);
}

function getSummary() {
  return loadOpsSection("summary");
}

function getQueue() {
  return loadOpsSection("queue");
}

async function triageQueueItem(itemId, input) {
  const payload = await window.OpsApi.opsFetch(`/api/ops/queue/${encodeURIComponent(itemId)}/triage`, {
    method: "POST",
    body: input,
  });
  if (!payload || !payload.item || !payload.auditEventId) {
    throw new Error("Invalid triage response shape");
  }
  return payload;
}

window.OpsDataSource = { OPS_ENDPOINTS, loadOpsSection, getSummary, getQueue, triageQueueItem };
