// HeroHuly.jsx — alternate hero with a "never-ending" light streak (Huly-style).
// Same copy as Hero.jsx; the background swaps the looping video for a layered
// system: a static lit arc (base art) + drifting ambient bloom + an SVG path
// carrying a bright white highlight that flows along the arc forever. The arc
// path is the exact curve from the source art (3832×1642), extended off-screen
// at both ends so the moving highlight is born and dies out of view.

function StarRating({ brand, alt, brandH, score, kind, base = 2.1, step = 0.15 }) {
  const STARS = 5;
  const [rated, setRated] = useState(false);
  const starsRef = useRef(null);
  useEffect(() => {
    const whiteAt = (base + (STARS - 1) * step + 0.5) * 1000;
    const t = setTimeout(() => setRated(true), whiteAt);
    return () => clearTimeout(t);
  }, []);
  // pointer-following spotlight: brand color blooms under the cursor across the stars
  const track = (e) => {
    const el = starsRef.current; if (!el) return;
    const r = el.getBoundingClientRect();
    el.style.setProperty("--mx", (e.clientX - r.left).toFixed(1) + "px");
    el.style.setProperty("--my", (e.clientY - r.top).toFixed(1) + "px");
  };
  const reset = () => {
    const el = starsRef.current; if (!el) return;
    el.style.setProperty("--mx", "-200px");
  };
  return (
    <div className={"rating-badge rating-anim badge-" + kind + (rated ? " is-rated" : "")}
      onPointerMove={track} onPointerLeave={reset}>
      <img className="rating-brand" src={assetUrl(brand)} alt={alt} style={{ height: brandH }} />
      <span className="rating-div" />
      <span ref={starsRef} className="rating-stars" aria-label={`${score} out of 5 stars`}>
        {Array.from({ length: STARS }).map((_, i) => (
          <span key={i} className="rstar" style={{ animationDelay: (base + i * step).toFixed(2) + "s" }}>★</span>
        ))}
      </span>
      <b className="rating-score" style={{ animationDelay: (base + (STARS - 1) * step + 0.55).toFixed(2) + "s" }}>{score}</b>
    </div>
  );
}

// arc from the source line.svg (frame 3832×1642), extended along its tangents
// past the top and bottom edges so the flowing highlight enters/exits off-view.
const HH_ARC = "M2808.4 -696.3 L2740.32 2.4 C2716.68 244.99 2494.09 912.66 1792.94 1642.59 L1024.4 2442.6";

function Hero() {
  // intro timeline: 0 pitch-black → 1 light grows → 2 surroundings appear → 3 streak flows
  // intro timeline: 0 pitch-black → 1 IMAGE loads in → 2 white streak appears → 3 streak flows.
  // gated on the base image's real load so the streak can never beat the image onscreen.
  const [stage, setStage] = useState(0);
  const [imgReady, setImgReady] = useState(false);
  const baseRef = useRef(null);
  useEffect(() => {
    const img = baseRef.current;
    if (img && img.complete && img.naturalWidth) setImgReady(true);
  }, []);
  useEffect(() => {
    if (!imgReady) return;
    if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) { setStage(3); return; }
    const t1 = setTimeout(() => setStage(1), 60);    // base image fades / grows in first
    const t2 = setTimeout(() => setStage(2), 1450);  // white-hot streak + bloom arrive on top
    const t3 = setTimeout(() => setStage(3), 2750);  // streak starts flowing + breathing begins
    return () => { clearTimeout(t1); clearTimeout(t2); clearTimeout(t3); };
  }, [imgReady]);

  return (
    <header id="top" className={"hh-hero hh-intro hh-s" + stage} style={{ position: "relative", overflow: "hidden", paddingTop: 74 }}>
      <div className="haze-bloom" style={{ opacity: 0.38 }} />

      {/* ── never-ending streak ─────────────────────────────────────────── */}
      <div className="hh-stage" aria-hidden="true">
        <div className="hh-layers">
          <div className="hh-bloom" />
          <img ref={baseRef} className="hh-base" src={assetUrl("assets/hero/streak-base.png")} alt="" onLoad={() => setImgReady(true)} />
          <svg className="hh-svg" viewBox="0 0 3832 1642" preserveAspectRatio="xMidYMid slice">
            <defs>
              <filter id="hhGlow" x="-40%" y="-40%" width="180%" height="180%">
                <feGaussianBlur stdDeviation="44" />
              </filter>
              <filter id="hhSoft" x="-30%" y="-30%" width="160%" height="160%">
                <feGaussianBlur stdDeviation="9" />
              </filter>
              <filter id="hhHot" x="-20%" y="-20%" width="140%" height="140%">
                <feGaussianBlur stdDeviation="3" />
              </filter>
            </defs>
            {/* static bloom + core reinforce the always-on arc */}
            <path className="hh-glow" pathLength="1000" filter="url(#hhGlow)" d={HH_ARC} />
            <path className="hh-core" pathLength="1000" filter="url(#hhSoft)" d={HH_ARC} />
            <path className="hh-core hh-hot" pathLength="1000" filter="url(#hhHot)" d={HH_ARC} />
            {/* flowing highlights — born off-screen top, die off-screen bottom */}
            <path className="hh-pulse hh-pulse-a" pathLength="1000" filter="url(#hhSoft)" d={HH_ARC} />
            <path className="hh-pulse hh-pulse-b" pathLength="1000" filter="url(#hhSoft)" d={HH_ARC} />
          </svg>
        </div>
      </div>

      <div className="wrap" style={{ position: "relative", zIndex: 2, paddingTop: 64, paddingBottom: 80 }}>
        <div className="hero-grid" style={{ display: "grid", gridTemplateColumns: "minmax(0,1fr) 560px", gap: 56, alignItems: "center" }}>
          <div>
            <div className="eyebrow">
              <span className="dot ld-word" style={{ animationDelay: ".2s" }} />
              <span><WordWaves text="Premium websites, built fast" base={0.2} step={0.08} /></span>
            </div>
            <h1 className="hero-h1" style={{
              fontFamily: "var(--font-display)", fontWeight: 600, fontSize: 56, lineHeight: 1.05,
              letterSpacing: "-0.02em", color: "var(--fg1)", margin: "20px 0 0", textWrap: "balance",
            }}>
              <WordWaves base={0.35} step={0.07} tokens={[
                { w: "A" }, { w: "website" }, { w: "that" }, { w: "matches" }, { w: "the" },
                { w: "quality" }, { w: "of" }, { w: "your" }, { w: "business." },
              ]} />
            </h1>
            <p className="hero-sub" style={{
              fontFamily: "var(--font-body)", fontWeight: 300, fontSize: 18, lineHeight: 1.6,
              color: "var(--fg2)", margin: "24px 0 0", maxWidth: 520,
            }}>
              <WordWaves base={0.7} step={0.05} text="Get a premium, conversion-focused website that sharpens your first impression, clarifies your offer, and gives visitors a better path to inquiry. Siteworks handles the strategy, build, launch, hosting, and updates so your team does not have to." />
            </p>
            <div className="hero-rise hero-cta-row" style={{ display: "flex", gap: 14, marginTop: 34, flexWrap: "wrap", animationDelay: "1.0s" }}>
              <a href="#cta" className="btn btn-primary">Get my homepage prototype <i className="ri-arrow-right-line" /></a>
              <a href="#review" className="btn btn-secondary">See pricing</a>
            </div>
            <div className="hero-rise hero-note" style={{ display: "flex", alignItems: "center", gap: 10, marginTop: 28, animationDelay: "1.15s" }}>
              <i className="ri-layout-4-line" style={{ color: "var(--violet-500)", fontSize: 18 }} />
              <span style={{ fontFamily: "var(--font-body)", fontWeight: 300, fontSize: 14, color: "var(--fg2)" }}>
                Share your current site. We&rsquo;ll prepare a homepage prototype before we meet.
              </span>
            </div>
            <div className="hero-rise hero-ratings" style={{ animationDelay: "1.3s" }}>
              <StarRating brand="assets/clutch-wordmark.png" alt="Clutch" brandH={15} score="5.0" kind="clutch" base={1.45} />
              <StarRating brand="assets/trustpilot-wordmark.png" alt="Trustpilot" brandH={14} score="4.8" kind="trustpilot" base={1.6} />
            </div>
          </div>
        </div>
      </div>
      <div className="streak" />
    </header>
  );
}

Object.assign(window, { Hero });
