// HeroStreak.jsx — Siteworks hero with a breathing solar halo.
// Same copy as Hero.jsx; the background swaps the looping video for a layered
// system: a static solar halo image + drifting ambient bloom + an SVG overlay
// carrying a bright horizontal highlight across the beam.

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>
  );
}

// Coordinates match assets/hero/halo-base.png (2400×1260), derived from the
// high-resolution Siteworks solar-beam share artwork.
const HH_BEAM = "M760 630 L2390 630";
const HH_RING = { cx: 1742, cy: 632, r: 350 };
const HH_FLOOR = { cx: 1742, cy: 925, rx: 560, ry: 70 };

function Hero() {
  // intro timeline: 0 dark → 1 ring draws in → 2 beam draws across → 3 ambient motion starts.
  // gated on the base image's real load so the final ambient layer never flashes in late.
  const [stage, setStage] = useState(0);
  const [imgReady, setImgReady] = useState(false);
  const baseRef = useRef(null);
  const ringOn = stage >= 1;
  const beamOn = stage >= 2;
  const environmentOn = stage >= 3;
  const ringStyle = { opacity: ringOn ? 1 : 0, strokeDashoffset: ringOn ? 0 : 1000 };
  const beamStyle = { opacity: beamOn ? 1 : 0, strokeDashoffset: beamOn ? 0 : 1000 };
  const environmentStyle = { opacity: environmentOn ? 1 : 0 };
  const pulseStyle = { strokeOpacity: environmentOn ? 1 : 0 };
  const floorStyle = {
    opacity: environmentOn ? 1 : ringOn ? 0.62 : 0,
    transform: ringOn ? "none" : "scaleX(.18)",
    transformOrigin: "72.5% 73%",
  };
  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), 80);    // solar ring appears first
    const t2 = setTimeout(() => setStage(2), 900);   // horizontal beam follows quickly
    const t3 = setTimeout(() => setStage(3), 1350);  // artwork arrives shortly after the ring
    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" style={environmentStyle} />
          <picture className="hh-base-wrap" style={environmentStyle}>
            <source
              type="image/avif"
              srcSet={[
                `${assetUrl("assets-min/hero/halo-base-768.avif")} 768w`,
                `${assetUrl("assets-min/hero/halo-base-1280.avif")} 1280w`,
                `${assetUrl("assets-min/hero/halo-base-1920.avif")} 1920w`,
              ].join(", ")}
              sizes="100vw"
            />
            <img ref={baseRef} className="hh-base" src={assetUrl("assets/hero/halo-base.png")} alt="" onLoad={() => setImgReady(true)} />
          </picture>
          <svg className="hh-svg" viewBox="0 0 2400 1260" preserveAspectRatio="xMidYMid slice">
            <defs>
              <filter id="hhGlow" x="-40%" y="-40%" width="180%" height="180%">
                <feGaussianBlur stdDeviation="36" />
              </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>
              <filter id="hhFloorBlur" x="-40%" y="-160%" width="180%" height="420%">
                <feGaussianBlur stdDeviation="20" />
              </filter>
            </defs>
            {/* static bloom + core reinforce the always-on solar halo */}
            <ellipse className="hh-floor-glow hh-glow" style={floorStyle} filter="url(#hhFloorBlur)" cx={HH_FLOOR.cx} cy={HH_FLOOR.cy} rx={HH_FLOOR.rx} ry={HH_FLOOR.ry} />
            <circle className="hh-ring-glow hh-glow" style={ringStyle} pathLength="1000" filter="url(#hhGlow)" cx={HH_RING.cx} cy={HH_RING.cy} r={HH_RING.r} />
            <circle className="hh-ring-core hh-core" style={ringStyle} pathLength="1000" filter="url(#hhSoft)" cx={HH_RING.cx} cy={HH_RING.cy} r={HH_RING.r} />
            <path className="hh-beam-glow hh-glow" style={beamStyle} pathLength="1000" filter="url(#hhGlow)" d={HH_BEAM} />
            <path className="hh-beam-core hh-core" style={beamStyle} pathLength="1000" filter="url(#hhSoft)" d={HH_BEAM} />
            <path className="hh-beam-hot hh-core hh-hot" style={beamStyle} pathLength="1000" filter="url(#hhHot)" d={HH_BEAM} />
            {/* flowing highlights — a white-hot pulse travels across the beam */}
            <path className="hh-pulse hh-pulse-a" style={pulseStyle} pathLength="1000" filter="url(#hhSoft)" d={HH_BEAM} />
            <path className="hh-pulse hh-pulse-b" style={pulseStyle} pathLength="1000" filter="url(#hhSoft)" d={HH_BEAM} />
          </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 });
