/* global React */
// ===== Books In The Loop — shared SVG graphics & icons =====
const { useEffect, useRef, useState } = React;

// --- Logo mark: Books in the Loop "comet" mark — a tapered fading loop + planet dot.
// (Our chosen brand mark, lifted from the full logo. Single gold accent color.) ---
function LogoMark({ size = 34, color, dot, stroke }) {
  const c = color || dot || "#d3a253";
  // sweep: tail at upper-left (~10–11 o'clock) clockwise over the top to the
  // head dot at lower-right (~4–5 o'clock).
  const A0 = (205 * Math.PI) / 180; // tail
  const A1 = (410 * Math.PI) / 180; // head (= 50°, one wrap past)
  const CX = 100, CY = 100, R = 70, N = 110;
  const dots = [];
  for (let i = 0; i <= N; i++) {
    const p = i / N;                       // 0 tail → 1 head
    const a = A0 + (A1 - A0) * p;
    const x = CX + R * Math.cos(a);
    const y = CY + R * Math.sin(a);
    const r = 0.45 + 3.1 * Math.pow(p, 1.7);
    const o = 0.04 + 0.96 * Math.pow(p, 1.35);
    dots.push(<circle key={i} cx={x} cy={y} r={r} fill={c} opacity={o} />);
  }
  const hx = CX + R * Math.cos(A1);
  const hy = CY + R * Math.sin(A1);
  return (
    <svg width={size} height={size} viewBox="0 0 200 200" fill="none" aria-hidden="true" style={{ display: "block", flex: "none" }}>
      {dots}
      <circle cx={hx} cy={hy} r="8.6" fill={c} />
    </svg>
  );
}

// --- Big animated orbit graphic for hero / process ---
function OrbitGraphic({ size = 460, variant = "default", spin = true }) {
  const c = size / 2;
  return (
    <svg width={size} height={size} viewBox="0 0 460 460" fill="none" aria-hidden="true"
         style={{ maxWidth: "100%", height: "auto" }}>
      <defs>
        <radialGradient id="og-core" cx="50%" cy="50%" r="50%">
          <stop offset="0%" stopColor="#1d3252" />
          <stop offset="100%" stopColor="#16273f" />
        </radialGradient>
        <linearGradient id="og-gold" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#e3b566" />
          <stop offset="100%" stopColor="#b8843a" />
        </linearGradient>
      </defs>

      {/* faint outer rings */}
      <circle cx={c} cy={c} r="218" stroke="rgba(227,181,102,0.12)" strokeWidth="1" />
      <circle cx={c} cy={c} r="170" stroke="rgba(247,243,234,0.08)" strokeWidth="1" />

      {/* main gold arc */}
      <circle cx={c} cy={c} r="195" stroke="url(#og-gold)" strokeWidth="2" strokeLinecap="round"
        strokeDasharray="900 1325" transform={`rotate(-60 ${c} ${c})`} opacity="0.95" />

      {/* dotted inner orbit */}
      <circle cx={c} cy={c} r="138" stroke="rgba(227,181,102,0.35)" strokeWidth="1.4" strokeDasharray="2 9" />

      {/* spinning dot group on main orbit */}
      <g className={spin ? "orbit__spin" : ""} style={{ transformOrigin: `${c}px ${c}px` }}>
        <circle cx={c + 195} cy={c} r="9" fill="url(#og-gold)" />
        <circle cx={c + 195} cy={c} r="16" fill="none" stroke="rgba(227,181,102,0.35)" strokeWidth="1" />
      </g>
      <g className={spin ? "orbit__spin" : ""} style={{ transformOrigin: `${c}px ${c}px`, animationDuration: "40s", animationDirection: "reverse" }}>
        <circle cx={c + 138} cy={c} r="4.5" fill="rgba(247,243,234,0.7)" />
      </g>

      {/* core */}
      <circle cx={c} cy={c} r="96" fill="url(#og-core)" stroke="rgba(227,181,102,0.25)" strokeWidth="1" />

      {/* core content: serif "B" monogram (our brand monogram) */}
      <text x={c} y={c + 2} textAnchor="middle" dominantBaseline="central"
        fontFamily="'Cormorant Garamond', Georgia, serif" fontSize="108" fontWeight="600"
        fill="#f7f3ea" letterSpacing="0.01em">B</text>
    </svg>
  );
}

// --- Compact orbit ring used behind centered hero text ---
function RingBehind({ size = 720, spin = true }) {
  const c = size / 2;
  return (
    <svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} fill="none" aria-hidden="true"
         style={{ position: "absolute", left: "50%", top: "50%", transform: "translate(-50%,-50%)", maxWidth: "118vw" }}>
      <defs>
        <linearGradient id="rb-gold" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#e3b566" />
          <stop offset="100%" stopColor="#b8843a" />
        </linearGradient>
      </defs>
      <circle cx={c} cy={c} r={c - 60} stroke="url(#rb-gold)" strokeWidth="1.6" opacity="0.65"
        strokeDasharray={`${2 * Math.PI * (c - 60) * 0.8} ${2 * Math.PI * (c - 60)}`} transform={`rotate(-55 ${c} ${c})`} />
      <circle cx={c} cy={c} r={c - 120} stroke="rgba(247,243,234,0.07)" strokeWidth="1" />
      <g className={spin ? "orbit__spin" : ""} style={{ transformOrigin: `${c}px ${c}px`, animationDuration: "32s" }}>
        <circle cx={c + (c - 60)} cy={c} r="8" fill="url(#rb-gold)" />
        <circle cx={c + (c - 60)} cy={c} r="15" fill="none" stroke="rgba(227,181,102,0.3)" strokeWidth="1" />
      </g>
    </svg>
  );
}

// --- Process orbit: 4 nodes around a ring, active node highlighted ---
function ProcessOrbit({ size = 420, active = 0, spin = true, onPick }) {
  const c = size / 2;
  const r = c - 46;
  const nodes = [-90, 0, 90, 180]; // top, right, bottom, left
  return (
    <svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} fill="none"
         style={{ maxWidth: "100%", height: "auto" }} aria-hidden="true">
      <defs>
        <linearGradient id="po-gold" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#e3b566" />
          <stop offset="100%" stopColor="#b8843a" />
        </linearGradient>
      </defs>
      <circle cx={c} cy={c} r={r} stroke="rgba(227,181,102,0.3)" strokeWidth="1.5" strokeDasharray="2 8" />
      <circle cx={c} cy={c} r={r - 30} stroke="rgba(247,243,234,0.06)" strokeWidth="1" />
      {/* center label */}
      <g transform={`translate(${c} ${c})`}>
        <circle r="58" fill="#16273f" stroke="rgba(227,181,102,0.25)" strokeWidth="1" />
        <text textAnchor="middle" y="-4" fontFamily="Cormorant Garamond, serif" fontSize="30" fill="#f7f3ea" fontWeight="600">0{active + 1}</text>
        <text textAnchor="middle" y="18" fontFamily="Hanken Grotesk, sans-serif" fontSize="9.5" letterSpacing="2.5" fill="rgba(227,181,102,0.85)">STEP</text>
      </g>
      {nodes.map((deg, i) => {
        const rad = (deg * Math.PI) / 180;
        const x = c + r * Math.cos(rad);
        const y = c + r * Math.sin(rad);
        const on = i === active;
        return (
          <g key={i} style={{ cursor: onPick ? "pointer" : "default" }} onClick={() => onPick && onPick(i)}>
            <circle cx={x} cy={y} r={on ? 22 : 15} fill={on ? "url(#po-gold)" : "#1d3252"}
              stroke={on ? "none" : "rgba(227,181,102,0.4)"} strokeWidth="1.4"
              style={{ transition: "all .4s cubic-bezier(.2,.7,.2,1)" }} />
            <text x={x} y={y + 5} textAnchor="middle" fontFamily="Cormorant Garamond, serif"
              fontSize={on ? 20 : 15} fill={on ? "#16273f" : "rgba(247,243,234,0.8)"} fontWeight="600"
              style={{ transition: "all .4s" }}>{i + 1}</text>
          </g>
        );
      })}
      {/* traveling micro-dot */}
      <g className={spin ? "orbit__spin" : ""} style={{ transformOrigin: `${c}px ${c}px`, animationDuration: "20s" }}>
        <circle cx={c + (r - 30)} cy={c} r="3.5" fill="rgba(247,243,234,0.6)" />
      </g>
    </svg>
  );
}

// --- Line icon set ---
const Icon = ({ name, size = 22, stroke = "currentColor", sw = 1.6 }) => {
  const p = { width: size, height: size, viewBox: "0 0 24 24", fill: "none",
    stroke, strokeWidth: sw, strokeLinecap: "round", strokeLinejoin: "round" };
  const paths = {
    ledger: <><rect x="4" y="3" width="16" height="18" rx="2" /><path d="M8 3v18M12 8h5M12 12h5M12 16h3" /></>,
    reconcile: <><path d="M4 7h11M4 7l3-3M4 7l3 3" /><path d="M20 17H9M20 17l-3-3M20 17l-3 3" /></>,
    invoice: <><path d="M6 2h9l4 4v16H6z" /><path d="M14 2v5h5M9 13h7M9 17h7M9 9h2" /></>,
    bill: <><rect x="3" y="6" width="18" height="13" rx="2" /><path d="M3 10h18M7 15h4" /></>,
    payroll: <><circle cx="12" cy="8" r="3.2" /><path d="M5 20a7 7 0 0 1 14 0" /><path d="M12 11.2V14" /></>,
    report: <><path d="M4 20V10M10 20V4M16 20v-7M22 20H2" /></>,
    cleanup: <><path d="M3 7l3 13h8l3-13" /><path d="M5 7l1.5-3h5L13 7M3 7h12M16 4l5 2-2 4-5-2" /></>,
    setup: <><path d="M12 2v3M12 19v3M4.2 4.2l2.1 2.1M17.7 17.7l2.1 2.1M2 12h3M19 12h3M4.2 19.8l2.1-2.1M17.7 6.3l2.1-2.1" /><circle cx="12" cy="12" r="3.4" /></>,
    tax1099: <><rect x="4" y="3" width="16" height="18" rx="2" /><path d="M8 8h8M8 12h8M8 16h5" /><circle cx="17" cy="17" r="3.5" fill="none" /></>,
    salestax: <><path d="M9 3H5a2 2 0 0 0-2 2v4l10 10 6-6L9 3z" /><circle cx="7.5" cy="7.5" r="1.3" /></>,
    cashflow: <><path d="M3 17c3 0 3-8 6-8s3 6 6 6 3-4 6-4" /><path d="M21 11v-3h-3" /></>,
    handoff: <><path d="M16 3h5v5M21 3l-7 7" /><path d="M21 14v5a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5" /></>,
    mail: <><rect x="3" y="5" width="18" height="14" rx="2" /><path d="m3 7 9 6 9-6" /></>,
    phone: <><path d="M5 4h4l1.5 5-2 1.5a11 11 0 0 0 5 5l1.5-2 5 1.5v4a2 2 0 0 1-2 2A16 16 0 0 1 3 6a2 2 0 0 1 2-2z" /></>,
    pin: <><path d="M12 21s7-6.2 7-11a7 7 0 0 0-14 0c0 4.8 7 11 7 11z" /><circle cx="12" cy="10" r="2.6" /></>,
    clock: <><circle cx="12" cy="12" r="9" /><path d="M12 7v5l3 2" /></>,
    check: <path d="M20 6 9 17l-5-5" />,
    arrow: <path d="M5 12h14M13 6l6 6-6 6" />,
    shield: <><path d="M12 3l7 3v5c0 4.5-3 8-7 10-4-2-7-5.5-7-10V6z" /><path d="m9 12 2 2 4-4" /></>,
    calendar: <><rect x="3" y="5" width="18" height="16" rx="2" /><path d="M3 9h18M8 3v4M16 3v4" /></>,
    chat: <><path d="M21 12a8 8 0 0 1-11.5 7.2L3 21l1.8-5.5A8 8 0 1 1 21 12z" /></>,
  };
  return <svg {...p}>{paths[name] || null}</svg>;
};

Object.assign(window, { LogoMark, OrbitGraphic, RingBehind, ProcessOrbit, Icon });
