// window.CFONotes — a collapsible "🎓 CFO Analysis" panel for the bottom of any
// page. It renders expert AI commentary for the page via the SECURE server-side
// synthesis path (window.PageCommentary → cfo-synthesis edge function), which
// holds the model key server-side and enforces guardrails.
//
// It deliberately does NOT call api.anthropic.com from the browser: that would
// require shipping an API key to the client (a security leak) and would be blocked
// by CORS. All AI here rides the same guardrailed pipeline as the rest of the app.
//
// Props: { pageType, tenantId, periodStart, periodEnd, title? }
//   pageType is passed through to PageCommentary (page_commentary surface). If the
//   synthesis backend has no catalog for that page_type, PageCommentary hides
//   itself silently — this panel then shows an honest "not available" line.

(function () {
  const h = React.createElement;
  const { useState } = React;

  function CFONotes(props) {
    const p = props || {};
    const [expanded, setExpanded] = useState(false);

    const header = h("div", {
      onClick: () => setExpanded(!expanded),
      style: { display: "flex", justifyContent: "space-between", alignItems: "center", padding: "12px 16px", background: "#f8faff", cursor: "pointer" },
    },
      h("div", { style: { display: "flex", alignItems: "center", gap: 8 } },
        h("span", { style: { fontSize: 16 } }, "🎓"),
        h("span", { style: { fontSize: 13, fontWeight: 700, color: "#0d2040" } }, p.title || "CFO Analysis"),
        h("span", { style: { fontSize: 11, color: "#9aacbf" } }, "AI-powered expert commentary")),
      h("span", { style: { color: "#00b894", fontSize: 13, fontWeight: 600 } }, expanded ? "▲ Collapse" : "▼ Expand"));

    let body = null;
    if (expanded) {
      const inner = (window.PageCommentary && p.tenantId && p.pageType)
        ? h(window.PageCommentary, { tenantId: p.tenantId, pageType: p.pageType, periodStart: p.periodStart, periodEnd: p.periodEnd })
        : h("div", { style: { fontSize: 12.5, color: "#6475a0", lineHeight: 1.6 } },
            "Expert commentary for this page isn't available yet. It appears automatically once the synthesis service has a briefing surface for “" + (p.pageType || "this page") + "”.");
      body = h("div", { style: { padding: "14px 16px" } }, inner);
    }

    return h("div", { style: { marginTop: 24, border: "1px solid #e0e8ff", borderRadius: 12, overflow: "hidden" } }, header, body);
  }

  window.CFONotes = CFONotes;
})();
