// live.jsx — wire the console to the live London API (same origin).
//
// The design reads everything from window.LondonData at render time, so we fetch
// real Bright Data intelligence, map it into the exact shapes the pages expect,
// and mutate window.LondonData. app.jsx calls window.loadLondonLive() on mount
// and re-renders. If the API is slow/unreachable, the mock seed stays — the UI
// never breaks.

(function () {
  function signalType(s) {
    s = (s || "").toLowerCase();
    if (/fund|series|seed|raise|round/.test(s)) return "funding";
    if (/hir|sales|gtm|recruit|account exec/.test(s)) return "hiring";
    if (/launch|product|releas|unveil|debut/.test(s)) return "tech";
    if (/partner|integration|alliance/.test(s)) return "expansion";
    if (/pric/.test(s)) return "pricing";
    if (/risk|churn/.test(s)) return "risk";
    return "tech";
  }
  function agentFor(t) {
    if (t === "pricing" || t === "tech") return "CMP";
    if (t === "risk") return "RNW";
    if (t === "expansion") return "MKT";
    return "SIG";
  }
  function pct(c) {
    return Math.round((typeof c === "number" ? c : 0.8) * 100);
  }

  async function jget(u) {
    const r = await fetch(u);
    if (!r.ok) throw new Error(r.status);
    return r.json();
  }
  async function jpost(u, b) {
    const r = await fetch(u, {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify(b),
    });
    if (!r.ok) throw new Error(r.status);
    return r.json();
  }

  window.LondonLiveStatus = { live: false, dataSource: "demo" };

  window.loadLondonLive = async function () {
    const D = window.LondonData;
    try {
      // 1) Live account signals → ACCOUNTS table + live FEED + execution TRACES
      const sig = await jpost("/api/tools/find_account_signals", {
        vertical: "AI security startups",
        signals: ["funding", "hiring"],
        limit: 10,
      });
      const results = (sig.data && sig.data.results) || [];

      if (results.length) {
        D.ACCOUNTS = results.map((r) => {
          const t = signalType(r.signal);
          const conf = pct(r.confidence);
          return {
            co: r.company,
            signal: r.signal,
            type: t,
            conf,
            source: (r.source || "").includes("Bright") ? "Bright Data" : r.source || "Bright Data",
            action: r.outboundAngle || "Trigger executive outreach",
            priority: conf >= 90 ? "high" : conf >= 80 ? "med" : "low",
            arr: "—",
          };
        });

        D.FEED_SEED = results.map((r, i) => {
          const t = signalType(r.signal);
          return {
            type: t,
            company: r.company,
            text: r.evidence || r.signal,
            conf: pct(r.confidence),
            source: "Bright Data",
            agent: agentFor(t),
            min: i * 3 + 1,
          };
        });
        // keep the streaming candidates fresh too (drawn from the tail)
        D.FEED_STREAM = results.slice(5).map((r) => {
          const t = signalType(r.signal);
          return {
            type: t,
            company: r.company,
            text: r.evidence || r.signal,
            conf: pct(r.confidence),
            source: "Bright Data",
            agent: agentFor(t),
          };
        });
      }

      // execution trace from the live Bright Data pipeline
      const trace = sig.trace || (sig.data && sig.data.trace);
      if (Array.isArray(trace) && trace.length) {
        const codes = ["PRX", "SIG", "OUT", "QAL", "CMP", "MKT"];
        const colors = ["#5B2BD9", "#0E8F9F", "#2563EB", "#0E9F6E", "#D98A0B", "#475569"];
        D.TRACES = trace.map((t, i) => ({
          agent: codes[i % codes.length],
          color: colors[i % colors.length],
          action: t.tool,
          detail: t.purpose,
          min: i * 2,
          status: t.status === "success" ? "done" : t.status === "skipped" ? "review" : "running",
        }));
      }

      window.LondonLiveStatus = { live: sig.dataSource === "live", dataSource: sig.dataSource || "demo" };

      // 2) Usage meter → KPI values (tasks automated, signals collected)
      try {
        const usage = await jget("/api/usage");
        D.KPIS = D.KPIS.map((k) => {
          if (k.key === "tasks") return { ...k, value: (usage.calls || 0) * 1000 + k.value };
          if (k.key === "signals" && results.length)
            return { ...k, value: Math.max(k.value, results.length * 1000) };
          return k;
        });
      } catch (e) {}

      // 3) Bright Data integration → reflect live connection
      if (Array.isArray(D.INTEGRATIONS)) {
        D.INTEGRATIONS = D.INTEGRATIONS.map((it) =>
          it.name === "Bright Data"
            ? { ...it, status: "healthy", desc: "Live web intelligence — connected" }
            : it,
        );
      }

      // 4) Agent marketplace → mark the curated workforce as live where it maps
      //    to a Bright Data–powered marketplace agent.
      //
      //    IMPORTANT: do NOT replace D.CATALOG / D.DEPARTMENTS here. The console's
      //    Workspace recommendations (workspace.jsx) and the Workforce Catalog
      //    (marketplace.jsx) are curated views keyed by the fixed agent codes
      //    defined in data.jsx (PRX, SIG, OUT, CMP, QAL, RNW, MKT, BRF, FCT,
      //    ENR, ADV, NEG). Overwriting the catalog with API-derived codes makes
      //    every agentByCode()/CATALOG.find(code) lookup miss, which silently
      //    empties both the main page and the catalog. We only enrich in place.
      try {
        const mk = await jget("/api/marketplace");
        const liveNames = new Set();
        (mk.categories || []).forEach((c) =>
          (c.agents || []).forEach((a) => {
            if (a.live) liveNames.add(String(a.name).toLowerCase());
          }),
        );
        if (liveNames.size && Array.isArray(D.CATALOG)) {
          D.CATALOG = D.CATALOG.map((agent) =>
            liveNames.has(String(agent.name).toLowerCase())
              ? { ...agent, installed: true }
              : agent,
          );
        }
      } catch (e) {}
    } catch (e) {
      // API unreachable → keep the mock seed; UI still renders.
      console.warn("[london] live data unavailable, using demo seed:", e && e.message);
    }
    return window.LondonData;
  };
})();
