// AI maturity — MIT CISR Enterprise AI Maturity Model, told through one
// illustrative business ("Harbour & Co.") that grows through all four stages,
// plus the "how we partner" section (consulting + products).
// Styles: maturity.css (prefix .mx-). Builds on home.css + home-bold.css.

const MX_PRODUCTS = {
  hyperaccounts: { name: 'HyperAccounts', d: 'Your bills and invoices in the AI front end you already use, via MCP.', href: 'hyperaccounts.html' },
  localai: { name: 'Local AI', d: 'Cloud and on-premise AI that keeps you working if frontier AI services go offline.', href: 'local-ai.html' },
};

const MX_STAGES = [
  {
    n: 1, name: 'Experiment and Prepare', focus: 'Exploration & education', pct: 13, pct22: 28, growth: -26.5, profit: -15.1,
    mit: ['Educate the workforce on AI', 'Set acceptable-use policies', 'Make data accessible', 'Decide where humans stay in the loop'],
    ex: {
      title: 'Everyone’s trying AI with no guidelines or principles',
      body: 'Staff at Harbour & Co. use free chatbots on their own to draft emails and quotes. The owner worries customer pricing is being pasted into public tools, and supplier bills still sit in a shared inbox.',
      moves: ['Writes a one-page AI acceptable-use policy', 'Chooses one business AI front end for the whole team', 'Runs hands-on training for every role', 'Moves bills and invoices out of inboxes and into the accounting system'],
      outcome: 'Safe, shared AI use, and data that can finally be found.',
    },
    help: {
      consult: ['AI readiness and data review', 'Acceptable-use policy and team training', 'Vendor-neutral choice of AI front end', 'A first pilot with a real business case'],
      products: ['hyperaccounts'],
    },
  },
  {
    n: 2, name: 'Build Pilots and Capabilities', focus: 'Business cases & pilots', pct: 23, pct22: 34, growth: -6.8, profit: -1.4,
    mit: ['Run AI pilots with clear business cases', 'Simplify and automate processes', 'Share data through APIs', 'Use LLMs to augment work'],
    ex: {
      title: 'One pilot, measured properly.',
      body: 'Harbour simplifies its bill approvals first, then connects its accounts to the team’s AI front end. Now the finance lead just asks: “Which supplier bills are due this week, and which customers are over 60 days?”',
      moves: ['Maps and simplifies payables and receivables', 'Connects bills and invoices to AI via MCP (HyperAccounts)', 'Tracks hours saved against the business case', 'Builds in-house AI champions'],
      outcome: 'A measured win the business can point to, and a template for the next one.',
    },
    help: {
      consult: ['Simplify the process before automating it', 'MCP and API integration with your enterprise apps', 'Pilot design with success measures', 'Capability building for your AI champions'],
      products: ['hyperaccounts', 'localai'],
    },
  },
  {
    n: 3, name: 'Develop AI Ways of Working', focus: 'Scaling AI platforms & dashboards', pct: 46, pct22: 31, growth: 4.7, profit: 0.8,
    mit: ['Build a scalable, reusable AI architecture', 'Expand automation across the business', 'Adopt a test-and-learn culture', 'Explore autonomous agents'],
    ex: {
      title: 'AI becomes how the business works.',
      body: 'Harbour connects accounting, CRM, inventory and email to one AI layer. Agents chase overdue invoices and turn emailed orders into draft sales orders, and a dashboard shows everything they did. A local AI workstation keeps orders moving if a cloud AI service goes down.',
      moves: ['One shared AI platform across every core app', 'Agents for invoice chasing and order entry', 'Dashboards and monthly test-and-learn reviews', 'Local AI fallback for business continuity'],
      outcome: 'Reusable AI across the business, owned and run by Harbour’s own team.',
    },
    help: {
      consult: ['AI architecture across your enterprise apps', 'Agent design with humans in the loop', 'Governance, dashboards and an AI operating rhythm', 'Resilience planning for AI outages'],
      products: ['hyperaccounts', 'localai'],
    },
  },
  {
    n: 4, name: 'Become AI Future Ready', focus: 'Continuous innovation & new revenue', pct: 18, pct22: 7, growth: 13.9, profit: 9.9,
    mit: ['Embed AI in decision-making', 'Sell AI-augmented products and services', 'Combine traditional, generative, agentic and robotic AI', 'Innovate continuously'],
    ex: {
      title: 'AI creates new value, for customers too.',
      body: 'Harbour’s café and restaurant customers now order, check stock and query their accounts through a 24/7 AI assistant, a service its competitors can’t match. Weekly pricing and reorder decisions start from AI recommendations, and Harbour’s team builds new agents themselves.',
      moves: ['Customer-facing AI ordering and account service', 'AI recommendations behind pricing and reorder decisions', 'The team builds and improves its own agents', 'An innovation backlog reviewed every quarter'],
      outcome: 'AI as a competitive advantage and a new revenue stream.',
    },
    help: {
      consult: ['Designing AI-augmented services for your customers', 'Embedding AI in everyday decisions', 'Advisory on call: you run it, we back you'],
      products: ['localai'],
    },
  },
];

// ── The example business, drawn. Every element switches on at the stage it
// arrives in, so the same picture visibly grows from stage 1 to 4. ─────────
function MxDiagram({ stage }) {
  const on = (cond) => 'mx-el' + (cond ? ' on' : '');
  const people = [140, 195, 250, 305];
  const apps = [
    { y: 80, label: 'Accounting', s: 2 },
    { y: 170, label: 'CRM', s: 3 },
    { y: 260, label: 'Inventory', s: 3 },
    { y: 350, label: 'Email & docs', s: 3 },
  ];
  const hx = 330, hy = 222;
  const labels = {
    1: 'Stage 1: personal AI apps, siloed systems, a policy in place',
    2: 'Stage 2: one shared AI front end, connected to accounting via MCP',
    3: 'Stage 3: every core app connected, agents, a dashboard and a local AI fallback',
    4: 'Stage 4: AI in every decision and a 24/7 AI service sold to customers',
  };
  return (
    <svg className="mx-diagram" viewBox="0 0 680 440" role="img" aria-label={'Harbour & Co. at ' + labels[stage]}>
      {/* policy pill — arrives at stage 1 and stays */}
      <g>
        <rect className="mx-pill" x="24" y="26" width="132" height="30" rx="15" />
        <text className="mx-t-sm mx-t-strong" x="90" y="45" textAnchor="middle">AI policy ✓</text>
      </g>

      {/* team */}
      <text className="mx-t-lab" x="80" y="106" textAnchor="middle">TEAM</text>
      {people.map((y) => <circle key={y} className="mx-person" cx="80" cy={y} r="15" />)}

      {/* stage 1: everyone on their own personal AI app */}
      <g className={on(stage === 1)}>
        {people.map((y) => (
          <g key={y}>
            <line className="mx-link-faint" x1="95" y1={y} x2="148" y2={y} />
            <circle className="mx-pers-ai" cx="158" cy={y} r="9" />
          </g>
        ))}
        <text className="mx-t-sm" x="158" y="340" textAnchor="middle">personal AI apps</text>
        <circle className="mx-hub-ghost" cx={hx} cy={hy} r="44" />
        <text className="mx-t-sm" x={hx} y={hy - 4} textAnchor="middle">no shared</text>
        <text className="mx-t-sm" x={hx} y={hy + 12} textAnchor="middle">AI yet</text>
      </g>

      {/* stage 2+: team → shared AI front end */}
      <g className={on(stage >= 2)}>
        {people.map((y) => <line key={y} className="mx-link mx-flow" x1="96" y1={y} x2={hx - 46} y2={hy} />)}
      </g>

      {/* stage 4: decision ring */}
      <g className={on(stage >= 4)}>
        <circle className="mx-ring" cx={hx} cy={hy} r="82" />
        <text className="mx-t-sm mx-t-sig" x={hx} y="128" textAnchor="middle">AI in every decision</text>
      </g>

      {/* stage 3+: agents orbiting the hub */}
      <g className={on(stage >= 3)}>
        <g className="mx-orbit">
          {[0, 120, 240].map((a) => {
            const r = 64, rad = (a - 90) * Math.PI / 180;
            return <circle key={a} className="mx-agent" cx={hx + r * Math.cos(rad)} cy={hy + r * Math.sin(rad)} r="8" />;
          })}
        </g>
        <text className="mx-t-sm" x="398" y="304" textAnchor="start">agents</text>
      </g>

      {/* hub */}
      <g className={on(stage >= 2)}>
        <circle className="mx-hub" cx={hx} cy={hy} r="44" />
        <text className="mx-t-hub" x={hx} y={hy - 3} textAnchor="middle">Your AI</text>
        <text className="mx-t-hub" x={hx} y={hy + 14} textAnchor="middle">front end</text>
      </g>

      {/* hub → apps */}
      {apps.map((a) => (
        <path key={a.label} className={on(stage >= a.s) + ' mx-link mx-flow'}
          d={`M${hx + 44} ${hy} C ${hx + 100} ${hy}, ${hx + 110} ${a.y}, 495 ${a.y}`} />
      ))}
      <text className={on(stage >= 2) + ' mx-t-sm mx-t-acc'} x="560" y="120" textAnchor="middle">via HyperAccounts MCP</text>

      {/* apps */}
      <text className="mx-t-lab" x="560" y="44" textAnchor="middle">YOUR APPS</text>
      {apps.map((a) => (
        <g key={a.label}>
          <rect className={'mx-app' + (stage >= a.s ? ' live' : '')} x="495" y={a.y - 22} width="130" height="44" rx="10" />
          <text className="mx-t-app" x="560" y={a.y + 5} textAnchor="middle">{a.label}</text>
        </g>
      ))}

      {/* stage 3+: dashboard + local AI fallback */}
      <g className={on(stage >= 3)}>
        <line className="mx-link" x1={hx} y1="84" x2={hx} y2={hy - 46} />
        <rect className="mx-box" x={hx - 62} y="40" width="124" height="44" rx="10" />
        <text className="mx-t-app" x={hx} y="67" textAnchor="middle">Dashboard</text>
        <line className="mx-link-dash" x1={hx} y1={hy + 46} x2={hx} y2="364" />
        <rect className="mx-box mx-box-local" x={hx - 72} y="364" width="144" height="44" rx="10" />
        <text className="mx-t-app" x={hx} y="391" textAnchor="middle">Local AI fallback</text>
      </g>

      {/* stage 4: customers + new revenue */}
      <g className={on(stage >= 4)}>
        <path className="mx-link mx-link-sig mx-flow" d={`M${hx - 40} ${hy + 20} C 230 330, 190 395, 162 400`} />
        <rect className="mx-box mx-box-sig" x="20" y="378" width="142" height="44" rx="10" />
        <text className="mx-t-app" x="91" y="398" textAnchor="middle">Customers</text>
        <text className="mx-t-xs" x="91" y="412" textAnchor="middle">24/7 AI service</text>
        <rect className="mx-pill-sig" x="190" y="300" width="118" height="26" rx="13" />
        <text className="mx-t-sm mx-t-strong" x="249" y="317" textAnchor="middle">+ new revenue</text>
      </g>
    </svg>
  );
}

// ── MIT CISR financial performance by stage, as a diverging bar chart ──────
function MxPerfChart({ stage, onPick }) {
  const base = 130, k = 4.4;
  return (
    <svg className="mx-perf" viewBox="0 0 640 320" role="img"
      aria-label="Growth versus industry average by stage: Stage 1 minus 26.5 points, Stage 2 minus 6.8, Stage 3 plus 4.7, Stage 4 plus 13.9">
      <line className="mx-base" x1="30" y1={base} x2="640" y2={base} />
      <text className="mx-t-xs" x="640" y={base + 18} textAnchor="end">industry average</text>
      {MX_STAGES.map((s, i) => {
        const x = 60 + i * 145, w = 90, h = Math.abs(s.growth) * k;
        const y = s.growth < 0 ? base : base - h;
        const vy = s.growth < 0 ? base + h + 20 : base - h - 10;
        return (
          <g key={s.n} className={'mx-bar-g' + (stage === s.n ? ' cur' : '')} onClick={() => onPick(s.n)}>
            <rect className={s.growth < 0 ? 'mx-bar-neg' : 'mx-bar-pos'} x={x} y={y} width={w} height={h} rx="6" />
            <text className="mx-t-val" x={x + w / 2} y={vy} textAnchor="middle">{(s.growth > 0 ? '+' : '−') + Math.abs(s.growth)}</text>
            <text className="mx-t-sm" x={x + w / 2} y="308" textAnchor="middle">Stage {s.n}</text>
          </g>
        );
      })}
    </svg>
  );
}

function MaturitySection() {
  const [stage, setStage] = React.useState(1);
  const s = MX_STAGES[stage - 1];
  const next = MX_STAGES[stage];
  const goTo = (n) => {
    setStage(n);
    const el = document.getElementById('mx-panel');
    if (el && el.getBoundingClientRect().top < 0) el.scrollIntoView({ behavior: 'smooth', block: 'start' });
  };
  return (
    <section id="maturity" className="hb hb-sec mx">
      <div className="hm-wrap">
        <div className="hb-kicker"><span className="hm-tag"><b>02</b>Where are you on the AI journey?</span><span className="count">MIT CISR Enterprise AI Maturity Model</span></div>
        <FadeUp>
          <div className="hb-intro">
            <h2 className="hb-h2">Four stages of AI maturity. <span className="acc">One business growing through them.</span></h2>
            <p>MIT CISR’s Enterprise AI Maturity Model describes four stages. In its 2025 survey, businesses at stages 1 and 2 trailed their industry’s financial performance, while those at stages 3 and 4 beat it. Follow one business, Harbour &amp; Co., as it grows through each stage.</p>
          </div>
        </FadeUp>

        {/* staircase — the stages themselves, and the stage picker */}
        <div className="mx-stairs" aria-label="Choose a stage">
          {MX_STAGES.map((st) => (
            <button key={st.n} type="button" className={'mx-step s' + st.n + (stage === st.n ? ' cur' : '')}
              aria-pressed={stage === st.n} onClick={() => setStage(st.n)}>
              <span className="k">Stage 0{st.n}</span>
              <span className="nm">{st.name}</span>
              <span className="pc"><b>{st.pct}%</b> of companies · was {st.pct22}% in 2022</span>
            </button>
          ))}
        </div>

        {/* the example, at the chosen stage */}
        <div id="mx-panel" className="mx-panel" aria-live="polite">
          <div className="mx-fig">
            <div className="mx-fig-head">
              <span className="hm-tag"><b>//</b>Harbour &amp; Co. · Stage {stage}</span>
              <span className="mx-ill">Illustrative example</span>
            </div>
            <MxDiagram stage={stage} />
            <p className="mx-fig-note">A 35-person wholesale distributor of hospitality supplies, running accounting, CRM, inventory and Microsoft 365.</p>
          </div>
          <div className="mx-copy" key={stage}>
            <span className="mx-focus">MIT focus · {s.focus}</span>
            <h3>{s.ex.title}</h3>
            <p className="mx-body">{s.ex.body}</p>
            <div className="mx-cols">
              <div>
                <div className="mx-sub">What Harbour does</div>
                <ul className="mx-list">{s.ex.moves.map((m) => <li key={m}>{m}</li>)}</ul>
              </div>
              <div>
                <div className="mx-sub">MIT CISR: companies at this stage</div>
                <ul className="mx-list mx-list-mit">{s.mit.map((m) => <li key={m}>{m}</li>)}</ul>
              </div>
            </div>
            <div className="mx-outcome"><i></i><span>{s.ex.outcome}</span></div>
          </div>
        </div>

        {/* how we help from here */}
        <div className="mx-next">
          <div className="mx-next-head">
            <span className="hm-tag"><b>→</b>{next ? `From stage ${stage} to stage ${next.n} with HyperFocusOZ` : 'Staying future-ready with HyperFocusOZ'}</span>
            <h3>{next ? `Next: ${next.name}.` : 'Keep innovating. On your terms.'}</h3>
          </div>
          <div className="mx-next-grid">
            <div className="mx-next-c">
              <div className="mx-sub">Consulting</div>
              <ul className="mx-list">{s.help.consult.map((c) => <li key={c}>{c}</li>)}</ul>
            </div>
            <div className="mx-next-c">
              <div className="mx-sub">Products</div>
              <div className="mx-prods">
                {s.help.products.map((p) => (
                  <a key={p} className="mx-prod" href={MX_PRODUCTS[p].href}>
                    <strong>{MX_PRODUCTS[p].name}</strong><span>{MX_PRODUCTS[p].d}</span><Arrow size={16} />
                  </a>
                ))}
              </div>
            </div>
            <div className="mx-next-cta">
              {next
                ? <button type="button" className="hm-btn hm-btn-acc" onClick={() => goTo(next.n)}>See Harbour at stage {next.n} <Arrow /></button>
                : <button type="button" className="hm-btn hm-btn-line" onClick={() => goTo(1)}>Back to stage 1</button>}
              <a className="hm-btn hm-btn-line" href="#book" data-booking="discovery">Find your stage: book a call</a>
            </div>
          </div>
        </div>

        {/* why it matters */}
        <div className="mx-why">
          <div>
            <span className="hm-tag"><b>//</b>Why move up a stage</span>
            <h3>Maturity shows up in the numbers.</h3>
            <p>Growth relative to industry average, in percentage points, by stage. Profit follows the same pattern, from −15.1 points at stage 1 to +9.9 at stage 4. And the field is moving: the share of companies at stage 3 or 4 rose from 38% in 2022 to 64% in 2025.</p>
            <dl className="mx-why-cur">
              <div><dt>Stage {stage} growth</dt><dd>{(s.growth > 0 ? '+' : '−') + Math.abs(s.growth)} pts</dd></div>
              <div><dt>Stage {stage} profit</dt><dd>{(s.profit > 0 ? '+' : '−') + Math.abs(s.profit)} pts</dd></div>
              <div><dt>Companies here</dt><dd>{s.pct}%</dd></div>
            </dl>
          </div>
          <MxPerfChart stage={stage} onPick={setStage} />
        </div>
        <p className="mx-src">Source: MIT CISR Enterprise AI Maturity Model (Weill, Woerner &amp; Sebastian, December 2024) and “Grow Enterprise AI Maturity for Bottom-Line Impact” (Woerner, Sebastian, Weill &amp; Kaganer, August 2025). Shares of companies and performance from the MIT CISR 2025 Real-Time Business Survey (N=152; growth and profit measured in 2024); 2022 shares from the MIT CISR 2022 Future Ready Survey (N=721). Harbour &amp; Co. is an illustrative example, not a client.</p>
      </div>
    </section>
  );
}

// ── How we partner: consulting that leaves you in control + products ──────
function PartnerSection() {
  const fronts = ['ChatGPT', 'Claude', 'Gemini', 'Any MCP client'];
  return (
    <section id="partner" className="hb hb-sec mx mx-partner">
      <div className="hm-wrap">
        <div className="hb-kicker"><span className="hm-tag"><b>03</b>How we partner</span><span className="count">Whatever stage you’re at</span></div>
        <FadeUp>
          <div className="hb-intro">
            <h2 className="hb-h2">Wherever you are, <span className="acc">we’ll get you to the next stage.</span></h2>
            <p>Two ways we work with you, used together or on their own. Either way, you end up running your own AI, not renting ours.</p>
          </div>
        </FadeUp>
        <div className="mx-ways">
          <FadeUp from="left">
            <div className="mx-way inv">
              <span className="lbl">01 · Consulting</span>
              <h3>Put your business in control of AI.</h3>
              <p>We’re not a managed service you can’t leave. We design and build alongside your team, then hand everything over: the architecture, the agents, the documentation and the know-how.</p>
              <ul className="mx-checks">
                <li>Your accounts, your keys, your data. Nothing locked to us.</li>
                <li>Vendor-neutral: we pick the right AI for the job, not a reseller deal.</li>
                <li>Your team trained to run, change and extend what we build.</li>
                <li>Optional support when you want it, never because you have to.</li>
              </ul>
              <a className="hm-btn hb-btn-sig" href="#book" data-booking="discovery">Talk to us about consulting <Arrow /></a>
            </div>
          </FadeUp>
          <FadeUp from="right" delay={80}>
            <div className="mx-way lite">
              <span className="lbl">02 · Products</span>
              <h3>Pre-built AI tools that plug into your business.</h3>
              <a className="mx-prodcard" href="hyperaccounts.html">
                <div className="mx-prodcard-top"><strong>HyperAccounts</strong><Arrow size={16} /></div>
                <p>Brings your bills and invoices to the AI front end of your choice through MCP. Ask about what you owe and who owes you, right where you already work.</p>
                <div className="mx-mcp" aria-label="Bills and invoices flow through HyperAccounts MCP to ChatGPT, Claude, Gemini or any MCP client">
                  <span className="src">Bills &amp; invoices</span>
                  <span className="pipe" aria-hidden="true"></span>
                  <span className="mid">MCP</span>
                  <span className="pipe" aria-hidden="true"></span>
                  <span className="dst">{fronts.map((f) => <em key={f}>{f}</em>)}</span>
                </div>
              </a>
              <a className="mx-prodcard" href="local-ai.html">
                <div className="mx-prodcard-top"><strong>Local AI</strong><Arrow size={16} /></div>
                <p>Cloud and on-premise hardware setups running your own models, designed to keep your business working if frontier AI services go offline.</p>
                <div className="mx-fail" aria-label="If frontier AI goes offline, work fails over to local AI">
                  <span className="off"><i></i>Frontier AI offline</span>
                  <span className="arr" aria-hidden="true">→</span>
                  <span className="ok"><i></i>Local AI takes over</span>
                </div>
              </a>
            </div>
          </FadeUp>
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { MaturitySection, PartnerSection, MxDiagram, MxPerfChart, MX_STAGES });
