// ============================================================
// autoclient.io: INTERACTIVE GROWTH ENGINE DIAGRAM
// Visualizes acquisition channels -> AI core -> measurable outcomes
// ============================================================
const { useState: useGES, useEffect: useGEE } = React;

const GE_CHANNELS = [
  {
    id: "google",
    icon: "mouse-pointer-click",
    name: "Google Ads",
    sub: "Search, Shopping & PMax",
    color: "#8784FD",
    outcomeIdx: 0,
    aiAction: "Predictive bidding scores search intent in real time, steering spend toward queries that close into signed revenue.",
    stat: "4.7× avg ROAS",
    path: "/services/google-ads",
  },
  {
    id: "social",
    icon: "megaphone",
    name: "Paid Social",
    sub: "Meta, TikTok & YouTube",
    color: "#CC99FF",
    outcomeIdx: 1,
    aiAction: "Algorithmic creative testing spots high-intent hooks early, scaling winners and cutting fatigued ads automatically.",
    stat: "38% lower CPL",
    path: "/services/paid-social",
  },
  {
    id: "seo",
    icon: "search",
    name: "SEO & Local SEO",
    sub: "Map pack & high-intent content",
    color: "#FF99CC",
    outcomeIdx: 2,
    aiAction: "Predictive demand modeling prioritizes the high-value local and commercial searches that compound organic pipeline.",
    stat: "180% organic lift",
    path: "/services/seo",
  },
  {
    id: "email",
    icon: "mail",
    name: "Email, SMS & CRM",
    sub: "Instant speed-to-lead flows",
    color: "#FF6666",
    outcomeIdx: 2,
    aiAction: "Intent-scored triggers follow up in seconds, nurturing high-value prospects into booked calls 24/7.",
    stat: "63% more bookings",
    path: "/services/email-marketing",
  },
];

const GE_OUTCOMES = [
  { label: "Blended ROAS", value: "4.7×", note: "Tied to signed revenue" },
  { label: "Cost per Lead", value: "-38%", note: "Waste cut automatically" },
  { label: "Booked Pipeline", value: "24/7", note: "Autonomous AI follow-up" },
];

// Cubic Bezier paths in a 400×310 coordinate space connecting left channels -> center (200,155) -> right outcomes
const IN_PATHS = [
  "M 0,38 C 95,38 115,148 160,155",
  "M 0,116 C 90,116 115,152 160,155",
  "M 0,194 C 90,194 115,158 160,155",
  "M 0,272 C 95,272 115,162 160,155",
];
const OUT_PATHS = [
  "M 240,155 C 290,150 310,55 400,55",
  "M 240,155 C 295,155 315,155 400,155",
  "M 240,155 C 290,160 310,255 400,255",
];

function GrowthEngine() {
  const [activeIdx, setActiveIdx] = useGES(0);
  const [paused, setPaused] = useGES(false);

  useGEE(() => {
    if (paused) return;
    const t = setInterval(() => setActiveIdx((i) => (i + 1) % GE_CHANNELS.length), 4200);
    return () => clearInterval(t);
  }, [paused]);

  const active = GE_CHANNELS[activeIdx];

  return (
    <section className="sec-sm">
      <div className="wrap-wide">
        <div
          className="reveal ge-shell surface-grain"
          onMouseEnter={() => setPaused(true)}
          onMouseLeave={() => setPaused(false)}
          onFocus={() => setPaused(true)}
          onBlur={() => setPaused(false)}
        >
          <Aura color="#8784FD" size={440} top={-200} left={-80} opacity={0.28} drift={1} />
          <Aura color="#FF6666" size={380} bottom={-210} right={-60} opacity={0.22} drift={2} />

          <div style={{ position: "relative", zIndex: 2, display: "flex", justifyContent: "space-between", alignItems: "flex-end", gap: 20, flexWrap: "wrap", marginBottom: 32 }}>
            <div>
              <Overline grad>Autonomous growth engine</Overline>
              <h2 style={{ fontWeight: 300, fontSize: "clamp(1.75rem,3vw,2.5rem)", lineHeight: 1.12, letterSpacing: "-0.02em", margin: "12px 0 0" }}>
                Every channel wired into <span className="gt">one AI brain</span>.
              </h2>
            </div>
            <p style={{ fontSize: 14.5, lineHeight: 1.6, color: "var(--fg-on-dark-2)", maxWidth: 400, margin: 0 }}>
              Predictive performance analytics target high-value intent across every channel—routing spend, creative, and follow-up into signed clients.
            </p>
          </div>

          <div className="ge-stage">
            {/* Left column: Acquisition channels */}
            <div className="ge-col" role="tablist" aria-label="Acquisition channels">
              {GE_CHANNELS.map((ch, i) => {
                const isOn = i === activeIdx;
                return (
                  <button
                    key={ch.id}
                    type="button"
                    role="tab"
                    aria-selected={isOn}
                    className={"ge-node" + (isOn ? " active" : "")}
                    onClick={() => setActiveIdx(i)}
                    onMouseEnter={() => setActiveIdx(i)}
                  >
                    <span className="ge-node-icon">
                      <Icon name={ch.icon} size={17} />
                    </span>
                    <span style={{ minWidth: 0 }}>
                      <span style={{ display: "block", fontSize: 14.5, fontWeight: 600, letterSpacing: "-0.01em" }}>{ch.name}</span>
                      <span style={{ display: "block", fontSize: 12, color: "var(--fg-on-dark-3)", marginTop: 2 }}>{ch.sub}</span>
                    </span>
                  </button>
                );
              })}
            </div>

            {/* Center: Animated SVG Bezier pipes + AI Brain */}
            <div className="ge-center-wrap">
              <svg className="ge-pipes" viewBox="0 0 400 310" preserveAspectRatio="none" aria-hidden="true">
                <defs>
                  <linearGradient id="ge-grad-out" x1="0" y1="0" x2="1" y2="0">
                    <stop offset="0%" stopColor="#8784FD" />
                    <stop offset="55%" stopColor="#FF99CC" />
                    <stop offset="100%" stopColor="#FF6666" />
                  </linearGradient>
                </defs>
                {IN_PATHS.map((d, i) => (
                  <g key={"in-" + i}>
                    <path d={d} className="ge-pipe-base" />
                    <path
                      d={d}
                      stroke={GE_CHANNELS[i].color}
                      className={"ge-pipe-flow" + (i === activeIdx ? " active" : "")}
                      style={{ animationDelay: `${i * -0.55}s` }}
                    />
                  </g>
                ))}
                {OUT_PATHS.map((d, i) => (
                  <g key={"out-" + i}>
                    <path d={d} className="ge-pipe-base" />
                    <path
                      d={d}
                      stroke="url(#ge-grad-out)"
                      className={"ge-pipe-flow" + (i === active.outcomeIdx ? " active" : "")}
                      style={{ animationDelay: `${i * -0.7}s` }}
                    />
                  </g>
                ))}
              </svg>

              <div className="ge-core">
                <div className="ge-core-ring" aria-hidden="true" />
                <div className="ge-core-ring2" aria-hidden="true" />
                <SymbolMark size={38} />
                <span style={{ fontSize: 10, fontWeight: 600, letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--auto-violet)", marginTop: 2 }}>
                  AI Brain
                </span>
              </div>
            </div>

            {/* Right column: Measurable outcomes */}
            <div className="ge-col ge-col-outcomes">
              {GE_OUTCOMES.map((o, i) => (
                <div key={o.label} className={"ge-outcome" + (i === active.outcomeIdx ? " lit" : "")}>
                  <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", gap: 8 }}>
                    <span className="gt" style={{ fontSize: 24, fontWeight: 600, letterSpacing: "-0.02em" }}>{o.value}</span>
                    <span style={{ fontSize: 11, fontWeight: 600, letterSpacing: "0.14em", textTransform: "uppercase", color: "var(--fg-on-dark-3)" }}>{o.label}</span>
                  </div>
                  <div style={{ fontSize: 12.5, color: "var(--fg-on-dark-2)", marginTop: 5 }}>{o.note}</div>
                </div>
              ))}
            </div>
          </div>

          {/* Live channel telemetry readout */}
          <div className="ge-readout" key={active.id}>
            <div style={{ display: "flex", alignItems: "center", gap: 14, flex: "1 1 380px" }}>
              <span className="pill pill-soft" style={{ flexShrink: 0, fontSize: 12.5 }}>
                <span className="ind-live-dot" style={{ marginRight: 2 }} />
                {active.name}
              </span>
              <span style={{ fontSize: 14.5, lineHeight: 1.55, color: "var(--fg-on-dark-1)" }}>{active.aiAction}</span>
            </div>
            <div style={{ display: "flex", alignItems: "center", gap: 18, flexShrink: 0 }}>
              <span className="gt" style={{ fontWeight: 700, fontSize: 15.5, letterSpacing: "-0.01em" }}>{active.stat}</span>
              <a href={active.path} className="arrow-link" style={{ fontSize: 13.5 }}>
                Explore <Icon name="arrow-right" size={15} />
              </a>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { GrowthEngine });
