const FS_B = {
  preSeed: { name: "Pre-Seed", rev: [0, 10000], team: [1, 3], prod: ["idea", "prototype", "mvp_pre_launch"], tr: ["none", "waitlist", "lois", "beta_users"], raise: "$50K - $500K", val: "$1M - $5M pre-money", eq: "5-15%", inst: "SAFE or Convertible Note" },
  seed: { name: "Seed", rev: [10000, 100000], team: [3, 10], prod: ["mvp_launched", "iterating", "v1"], tr: ["paying_customers", "early_pmf", "growing_mrr"], raise: "$500K - $3M", val: "$3M - $15M pre-money", eq: "15-25%", inst: "SAFE, Convertible Note, or Priced Round" },
  seriesA: { name: "Series A", rev: [100000, Infinity], team: [10, 30], prod: ["pmf_proven", "scaling", "mature"], tr: ["repeatable_sales", "strong_retention", "growth_engine"], raise: "$3M - $15M", val: "$15M - $50M pre-money", eq: "15-25%", inst: "Priced Equity Round (Series A Preferred)" },
};
const FS_PROD = [["idea", "Idea stage"], ["prototype", "Prototype built"], ["mvp_pre_launch", "MVP (pre-launch)"], ["mvp_launched", "MVP launched"], ["iterating", "Iterating on feedback"], ["v1", "V1 product"], ["pmf_proven", "Product-market fit proven"], ["scaling", "Scaling"], ["mature", "Mature product"]];
const FS_TR = [["none", "No traction yet"], ["waitlist", "Waitlist signups"], ["lois", "Letters of intent (LOIs)"], ["beta_users", "Beta users"], ["paying_customers", "Paying customers"], ["early_pmf", "Early PMF signals"], ["growing_mrr", "Growing MRR"], ["repeatable_sales", "Repeatable sales process"], ["strong_retention", "Strong retention metrics"], ["growth_engine", "Growth engine working"]];
const FS_ST = { match: ["Match", "g"], above: ["Above range", "n"], below: ["Below range", "r"] };

function StageCalc() {
  const [rv, setRv] = React.useState("");
  const [tm, setTm] = React.useState("");
  const [pd, setPd] = React.useState("");
  const [tr, setTr] = React.useState("");
  const R = parseNum(rv), T = parseNum(tm);
  const ok = tm !== "" && T >= 1 && pd !== "" && tr !== "";
  const res = React.useMemo(() => {
    if (!ok) return null;
    const AP = FS_PROD.map(x => x[0]), AT = FS_TR.map(x => x[0]), out = {};
    for (const k of ["preSeed", "seed", "seriesA"]) {
      const b = FS_B[k]; let s = 0; const bd = {};
      if (R >= b.rev[0] && R <= b.rev[1]) { s += 30; bd.revenue = [30, 30, "match"]; }
      else if (R < b.rev[0]) { const p = Math.round(Math.min(b.rev[0] > 0 ? R / b.rev[0] : 1, 1) * 30); s += p; bd.revenue = [p, 30, "below"]; }
      else bd.revenue = [0, 30, "above"];
      if (T >= b.team[0] && T <= b.team[1]) { s += 20; bd.team = [20, 20, "match"]; }
      else if (T < b.team[0]) { const p = Math.round(T / b.team[0] * 20); s += p; bd.team = [p, 20, "below"]; }
      else { s += 5; bd.team = [5, 20, "above"]; }
      if (b.prod.includes(pd)) { s += 25; bd.product = [25, 25, "match"]; }
      else { const ii = AP.indexOf(pd), mi = AP.indexOf(b.prod[0]); if (ii < mi) { const p = Math.round(Math.max(0, 1 - (mi - ii) / 3) * 25); s += p; bd.product = [p, 25, "below"]; } else { s += 5; bd.product = [5, 25, "above"]; } }
      if (b.tr.includes(tr)) { s += 25; bd.traction = [25, 25, "match"]; }
      else { const ii = AT.indexOf(tr), mi = AT.indexOf(b.tr[0]); if (ii < mi) { const p = Math.round(Math.max(0, 1 - (mi - ii) / 4) * 25); s += p; bd.traction = [p, 25, "below"]; } else { s += 5; bd.traction = [5, 25, "above"]; } }
      out[k] = { k, name: b.name, s, pct: Math.round(s / 100 * 10000) / 100, bd, b };
    }
    const rk = Object.values(out).sort((a, b) => b.s - a.s), between = rk[0].s - rk[1].s <= 10;
    return { out, best: rk[0], second: between ? rk[1] : null, between };
  }, [ok, R, T, pd, tr]);
  return (
    <div className="cx-stack">
      <div className="cx-split">
        <Box title="Inputs" sub="Enter your startup metrics below to find your best-fit fundraising stage with typical raise amounts, valuations, and instruments.">
          <Cells cols={2}><NumIn id="rv" label="Monthly Revenue (MRR)" pre="$" ph="0" value={rv} set={setRv} /><NumIn id="tm" label="Team Size" ph="2" value={tm} set={setTm} /></Cells>
          <Cells cols={2}><SelIn id="pd" label="Product Stage" value={pd} set={setPd} options={[["", "Select..."], ...FS_PROD]} /><SelIn id="tr" label="Traction Level" value={tr} set={setTr} options={[["", "Select..."], ...FS_TR]} /></Cells>
        </Box>
        <div className="cx-stick">
          <Box title="Results" right={<PdfButton ready={!!res} />}>
            {!res ? <Empty>Enter all required values above to see the calculated results.</Empty> : <>
              <Cells cols={2}><Res hi label="Best Fit Stage" value={res.best.name} /><Res label="Between Stages?" value={res.between ? `Yes — also consider ${res.second.name}` : "No — clear stage fit"} /></Cells>
              <Cells cols={2}><Res hi label="Typical Raise Range" value={res.best.b.raise} /><Res label="Typical Instrument" value={res.best.b.inst} /></Cells>
              <div className="fs-track">{["preSeed", "seed", "seriesA"].map(k => { const x = res.out[k]; return <div key={k} className={x === res.best ? "on" : ""}><span>{x.name}</span><div className="tr"><i style={{ width: x.pct + "%" }}></i></div><b>{x.pct}%</b></div>; })}</div>
            </>}
          </Box>
        </div>
      </div>
      {res && <div className="fs-grid">{["preSeed", "seed", "seriesA"].map(k => {
        const x = res.out[k];
        return (
          <Box key={k} className={x === res.best ? "fs-best" : ""} title={x.name} right={<span className="fs-pct">{x.pct}% fit</span>}>
            <ul className="cx-list fs-dims">{["revenue", "team", "product", "traction"].map(d => { const [p, m, st] = x.bd[d]; return <li key={d}><span style={{ textTransform: "capitalize" }}>{d}</span><span className="fs-r"><b>{p}/{m}</b><span className={"cx-tag " + FS_ST[st][1]}>{FS_ST[st][0]}</span></span></li>; })}</ul>
            <div className="fs-bm">{[["Raise", x.b.raise], ["Valuation", x.b.val], ["Equity Sold", x.b.eq], ["Instrument", x.b.inst]].map(([l, v]) => <div key={l}><span className="cx-lb">{l}</span><p>{v}</p></div>)}</div>
          </Box>
        );
      })}</div>}
    </div>
  );
}

mountCalc(
  <CalcPage slug="fundraising-stage-finder" title="Fundraising Stage" hl="Fit Finder"
    lead={<><strong>Knowing your stage</strong> is critical for targeting the right investors and setting realistic expectations. This tool scores your fit across Pre-Seed, Seed, and Series A based on revenue, team size, product maturity, and traction — then tells you where you stand and what to expect.</>}
    how={<>
      <h2>Understanding Fundraising Stages</h2>
      <p><strong>Pre-Seed</strong> is the earliest stage of institutional fundraising. Startups at this stage typically have $0-$10K in monthly recurring revenue, 1-3 founders, and are working on an idea, prototype, or pre-launch MVP. Raises range from $50K to $500K, usually via SAFEs or convertible notes, at $1M-$5M pre-money valuations. Investors at this stage bet on the team and the idea.</p>
      <p><strong>Seed</strong> stage companies have launched their MVP and are seeing early traction with paying customers. Revenue is typically $10K-$100K MRR, teams are 3-10 people, and raises range from $500K to $3M. Valuations range from $3M to $15M pre-money. At this stage, investors want to see product-market fit signals and a clear path to scalable growth.</p>
      <p><strong>Series A</strong> is for companies that have proven product-market fit and are ready to scale. Revenue exceeds $100K MRR ($1.2M+ ARR), teams are 10-30 people, and raises are $3M-$15M at $15M-$50M pre-money valuations. Series A investors expect repeatable sales processes, strong retention, and a clear growth engine. This round is typically a priced equity round with Series A Preferred stock.</p>
      <p><strong>Between stages?</strong> Many startups fall between two stages. If the calculator flags you as "between stages," it means your metrics are strong in some areas but not others. This is normal — use the detailed breakdown to identify which metrics need to improve before you clearly fit the next stage.</p>
    </>}>
    <StageCalc />
  </CalcPage>
);
