// Custom cursor — comic pointing-finger on hover
const { useEffect, useRef, useState } = React;

const HAND_SVG = (
  <svg className="cursor-hand-svg" viewBox="0 0 32 32" width="22" height="22" aria-hidden>
    {/* Crosshair / film viewfinder reticle — small, precise */}
    <circle cx="16" cy="16" r="13" fill="none" stroke="currentColor" strokeWidth="1" opacity="0.9" />
    <circle cx="16" cy="16" r="1.4" fill="currentColor" />
    <line x1="16" y1="2" x2="16" y2="8" stroke="currentColor" strokeWidth="1" />
    <line x1="16" y1="24" x2="16" y2="30" stroke="currentColor" strokeWidth="1" />
    <line x1="2" y1="16" x2="8" y2="16" stroke="currentColor" strokeWidth="1" />
    <line x1="24" y1="16" x2="30" y2="16" stroke="currentColor" strokeWidth="1" />
  </svg>
);

function CustomCursor({ style = "dot", accent = "#C7411E" }) {
  const dotRef = useRef(null);
  const ringRef = useRef(null);
  const handRef = useRef(null);
  const labelRef = useRef(null);
  const [mode, setMode] = useState("default");
  const [label, setLabel] = useState("");

  useEffect(() => {
    let mx = window.innerWidth / 2, my = window.innerHeight / 2;
    let rx = mx, ry = my;
    let rafId;

    const onMove = (e) => {
      mx = e.clientX; my = e.clientY;
      const dotT = `translate3d(${mx}px, ${my}px, 0) translate(-50%, -50%)`;
      if (dotRef.current) dotRef.current.style.transform = dotT;
  // Center the small reticle on the actual cursor point
  const handT = `translate3d(${mx - 11}px, ${my - 11}px, 0)`;
      if (handRef.current) handRef.current.style.transform = handT;
      if (labelRef.current) {
        labelRef.current.style.transform = `translate3d(${mx + 18}px, ${my + 18}px, 0)`;
      }
    };

    const tick = () => {
      rx += (mx - rx) * 0.18;
      ry += (my - ry) * 0.18;
      if (ringRef.current) {
        ringRef.current.style.transform = `translate3d(${rx}px, ${ry}px, 0) translate(-50%, -50%)`;
      }
      rafId = requestAnimationFrame(tick);
    };
    tick();

    const onOver = (e) => {
      const t = e.target.closest("[data-cursor]");
      if (t) {
        setMode(t.dataset.cursor || "link");
        setLabel(t.dataset.cursorLabel || "");
      } else {
        setMode("default");
        setLabel("");
      }
    };

    window.addEventListener("mousemove", onMove);
    window.addEventListener("mouseover", onOver);
    return () => {
      window.removeEventListener("mousemove", onMove);
      window.removeEventListener("mouseover", onOver);
      cancelAnimationFrame(rafId);
    };
  }, []);

  if (typeof window !== "undefined" && "ontouchstart" in window && navigator.maxTouchPoints > 0) {
    return null;
  }

  const isView = mode === "view";
  const isLink = mode === "link";
  const showHand = isView || isLink;

  return (
    <>
      <div
        ref={dotRef}
        className="cursor-dot"
        style={{
          background: accent,
          opacity: style === "ring" || showHand ? 0 : 1,
          width: 6,
          height: 6,
        }}
      />
      <div
        ref={ringRef}
        className="cursor-ring"
        style={{
          width: showHand ? 0 : (style === "ring" ? 22 : 28),
          height: showHand ? 0 : (style === "ring" ? 22 : 28),
          opacity: showHand ? 0 : 1,
          mixBlendMode: "difference",
        }}
      />
      <div
        ref={handRef}
        className={`cursor-hand ${showHand ? "is-on" : ""}`}
        style={{ color: isView ? accent : "var(--ink)" }}
        aria-hidden
      >
        {HAND_SVG}
        {isView && label && <span className="cursor-hand-tag" style={{ background: accent }}>{label}</span>}
      </div>
      {label && (
        <div ref={labelRef} className="cursor-label">{label}</div>
      )}
    </>
  );
}

window.CustomCursor = CustomCursor;
