// window.MetricTooltip — a small "?" info button that shows the banker/CFO
// definition, formula, interpretation and benchmarks for a metric. Self-contained
// shared component; drop next to any metric label:
//   h("span", null, "DSCR", h(window.MetricTooltip, { metric: "DSCR" }))

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

  const DEFINITIONS = {
    "DSCR": {
      name: "Debt Service Coverage Ratio",
      formula: "Net Operating Income ÷ Total Debt Service",
      interpretation: "Measures ability to service debt. Banks typically require ≥1.25×. Below 1.0× means cash flow cannot cover debt payments.",
      benchmarks: "Healthy: >1.25× | Watch: 1.0–1.25× | Critical: <1.0×",
    },
    "DSO": {
      name: "Days Sales Outstanding",
      formula: "(AR Balance ÷ TTM Revenue) × 365",
      interpretation: "Average days to collect payment after invoice. Lower is better. Rising DSO signals collection issues.",
      benchmarks: "Target: <45 days | Watch: 45–60 | Critical: >60",
    },
    "DPO": {
      name: "Days Payable Outstanding",
      formula: "(AP Balance ÷ TTM COGS) × 365",
      interpretation: "Average days to pay suppliers. Higher can improve cash flow but may strain supplier relationships.",
      benchmarks: "Industry median: 30–45 days",
    },
    "EBITDA": {
      name: "Earnings Before Interest, Taxes, Depreciation & Amortization",
      formula: "Revenue − COGS − Operating Expenses (excl. D&A, Interest, Tax)",
      interpretation: "Proxy for operating cash generation. Used by banks and PE firms for valuation multiples.",
      benchmarks: "Valuation: typically 4–8× EBITDA for SMBs",
    },
    "Gross Margin": {
      name: "Gross Profit Margin",
      formula: "(Revenue − COGS) ÷ Revenue",
      interpretation: "Percentage of revenue remaining after direct costs. Higher margins indicate pricing power or efficient production.",
      benchmarks: "Varies by industry — software: 70–80%, retail: 30–50%, services: 40–60%",
    },
    "Working Capital": {
      name: "Net Working Capital",
      formula: "Current Assets − Current Liabilities",
      interpretation: "Short-term financial health buffer. Positive = can meet near-term obligations. Banks monitor this closely.",
      benchmarks: "Target: positive and growing with revenue",
    },
    "Cash Conversion Cycle": {
      name: "Cash Conversion Cycle (CCC)",
      formula: "DSO + DIO − DPO",
      interpretation: "Days between paying for inputs and collecting from customers. Lower/negative is better for cash flow.",
      benchmarks: "World-class: <30 days. Negative CCC (e.g. Amazon) means customers fund operations.",
    },
    "Current Ratio": {
      name: "Current Ratio",
      formula: "Current Assets ÷ Current Liabilities",
      interpretation: "Ability to meet short-term obligations. Below 1.0 means current liabilities exceed current assets.",
      benchmarks: "Healthy: >2.0 | Adequate: 1.0–2.0 | Stressed: <1.0",
    },
    "Quick Ratio": {
      name: "Quick Ratio (Acid Test)",
      formula: "(Current Assets − Inventory) ÷ Current Liabilities",
      interpretation: "Stricter than current ratio — excludes inventory which may be slow to convert. Banks use this for liquidity assessment.",
      benchmarks: "Healthy: >1.0 | Watch: 0.5–1.0 | Stressed: <0.5",
    },
    "Leverage": {
      name: "Leverage Ratio",
      formula: "Total Debt ÷ EBITDA",
      interpretation: "How many years of EBITDA it would take to repay debt. Banks restrict new debt capacity as this climbs.",
      benchmarks: "Conservative: <2.5× | Moderate: 2.5–4.0× | High: >4.0×",
    },
    "Rule of 40": {
      name: "Rule of 40 (SaaS)",
      formula: "Revenue Growth % + EBITDA Margin %",
      interpretation: "SaaS benchmark: combined growth and profitability should exceed 40%. Balances growth investment vs profitability.",
      benchmarks: ">40: healthy | 20–40: developing | <20: needs attention",
    },
  };

  function MetricTooltip(props) {
    const metric = props && props.metric;
    const [show, setShow] = useState(false);
    const def = DEFINITIONS[metric];
    if (!def) return null;
    const btn = h("button", {
      onClick: (e) => { e.stopPropagation(); setShow(!show); },
      title: def.name,
      style: { background: "none", border: "1px solid #e0e8ff", borderRadius: "50%", width: 16, height: 16, fontSize: 9, cursor: "pointer", color: "#6475a0", fontWeight: 700, display: "inline-flex", alignItems: "center", justifyContent: "center", marginLeft: 4, flexShrink: 0, lineHeight: 1, padding: 0 },
    }, "?");
    const pop = show ? h("div", { style: { position: "absolute", zIndex: 500, left: 20, top: -8, width: 280, background: "#0d2040", borderRadius: 10, padding: "14px 16px", boxShadow: "0 8px 32px rgba(0,0,0,0.3)", textAlign: "left" }, onClick: (e) => e.stopPropagation() },
      h("div", { style: { fontSize: 12, fontWeight: 700, color: "white", marginBottom: 6, paddingRight: 14 } }, def.name),
      h("div", { style: { fontSize: 11, color: "rgba(255,255,255,0.6)", marginBottom: 6, fontFamily: "monospace" } }, "Formula: " + def.formula),
      h("div", { style: { fontSize: 11, color: "rgba(255,255,255,0.85)", lineHeight: 1.5, marginBottom: 6 } }, def.interpretation),
      h("div", { style: { fontSize: 10, color: "#00b894", fontWeight: 600 } }, "Benchmarks: " + def.benchmarks),
      h("button", { onClick: (e) => { e.stopPropagation(); setShow(false); }, "aria-label": "Close", style: { position: "absolute", top: 8, right: 8, background: "none", border: "none", color: "rgba(255,255,255,0.4)", cursor: "pointer", fontSize: 14, lineHeight: 1, padding: 0 } }, "×")) : null;
    return h("span", { style: { position: "relative", display: "inline-flex", alignItems: "center" } }, btn, pop);
  }

  MetricTooltip.DEFINITIONS = DEFINITIONS;
  window.MetricTooltip = MetricTooltip;
})();
