// Scroll-reveal + parallax + custom cursor — drop-in enhancers.
// Mounted from app.jsx via <ScrollFx /> and <CustomCursor />.

// ScrollFx: observer that adds .reveal-in to anything with .reveal once it scrolls into view,
// and applies a small parallax to elements with [data-parallax="N"] (N = px translation per 100px scroll).
const ScrollFx = () => {
  React.useEffect(() => {
    // Reveal observer
    const targets = document.querySelectorAll('.reveal');
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) {
          e.target.classList.add('reveal-in');
          io.unobserve(e.target);
        }
      });
    }, { threshold: 0.12, rootMargin: '0px 0px -80px 0px' });
    targets.forEach(t => io.observe(t));

    // Parallax via rAF
    const parTargets = Array.from(document.querySelectorAll('[data-parallax]'));
    let ticking = false;
    const apply = () => {
      ticking = false;
      const vh = window.innerHeight;
      parTargets.forEach(el => {
        const r = el.getBoundingClientRect();
        const center = r.top + r.height / 2;
        const offset = (center - vh / 2) / vh; // -1..+1 across viewport
        const factor = parseFloat(el.dataset.parallax) || 0.1;
        el.style.transform = `translate3d(0, ${(-offset * factor * 60).toFixed(1)}px, 0)`;
      });
    };
    const onScroll = () => { if (!ticking) { ticking = true; requestAnimationFrame(apply); } };
    window.addEventListener('scroll', onScroll, { passive: true });
    window.addEventListener('resize', onScroll);
    apply();

    return () => {
      io.disconnect();
      window.removeEventListener('scroll', onScroll);
      window.removeEventListener('resize', onScroll);
    };
  }, []);
  return null;
};

// CustomCursor — refined arrow. Familiar shape, thinner stroke, sharper tip,
// accent-tinted with a subtle hover state.
const CustomCursor = () => {
  const wrapRef = React.useRef(null);
  const stateRef = React.useRef({ x: -100, y: -100, hover: false, active: false, link: false });

  React.useEffect(() => {
    const isTouch = matchMedia('(pointer: coarse)').matches;
    if (isTouch) return;

    document.documentElement.classList.add('cursor-on');

    // Inject cursor:none via <style> tag — overrides inline React styles (cursor:'pointer')
    // CSS !important from an external file loses to inline styles; a dynamically injected
    // <style> in <head> with !important wins over everything.
    const cursorStyle = document.createElement('style');
    cursorStyle.id = '__mmrl-cursor-none';
    cursorStyle.textContent = 'html.cursor-on *, html.cursor-on *::before, html.cursor-on *::after { cursor: none !important; }';
    document.head.appendChild(cursorStyle);

    const s = stateRef.current;
    const onMove = (e) => { s.x = e.clientX; s.y = e.clientY; };
    const onDown = () => { s.active = true; };
    const onUp   = () => { s.active = false; };
    const onOver = (e) => {
      const t = e.target;
      const interactive = t.closest && t.closest('a, button, [role="button"], input, textarea, select, .cursor-hover');
      const link = t.closest && t.closest('a');
      s.hover = !!interactive;
      s.link = !!link;
    };
    const onLeave = () => { s.x = -200; s.y = -200; };

    window.addEventListener('mousemove', onMove);
    window.addEventListener('mousedown', onDown);
    window.addEventListener('mouseup', onUp);
    window.addEventListener('mouseover', onOver);
    window.addEventListener('mouseleave', onLeave);

    let raf;
    const tick = () => {
      const w = wrapRef.current;
      if (w) {
        // Small offset so the tip lands exactly on the pointer
        w.style.transform = `translate3d(${s.x - 2}px, ${s.y - 2}px, 0) scale(${s.active ? 0.86 : 1})`;
        w.dataset.hover = s.hover ? '1' : '0';
        w.dataset.link  = s.link  ? '1' : '0';
      }
      raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);

    return () => {
      cancelAnimationFrame(raf);
      window.removeEventListener('mousemove', onMove);
      window.removeEventListener('mousedown', onDown);
      window.removeEventListener('mouseup', onUp);
      window.removeEventListener('mouseover', onOver);
      window.removeEventListener('mouseleave', onLeave);
      document.getElementById('__mmrl-cursor-none')?.remove();
      document.documentElement.classList.remove('cursor-on');
    };
  }, []);

  return (
    <div ref={wrapRef} className="mmrl-arrow">
      <svg width="16" height="19" viewBox="0 0 22 26" fill="none">
        {/* Subtle outer halo on hover (drawn first, behind) */}
        <path className="mmrl-arrow-halo"
              d="M2 2 L2 20.2 L7.1 15.5 L10.4 22.4 L13.1 21.1 L9.85 14.3 L17.2 14.0 Z" />
        {/* Filled body */}
        <path className="mmrl-arrow-fill"
              d="M2 2 L2 20.2 L7.1 15.5 L10.4 22.4 L13.1 21.1 L9.85 14.3 L17.2 14.0 Z" />
        {/* Crisp outline */}
        <path className="mmrl-arrow-stroke"
              d="M2 2 L2 20.2 L7.1 15.5 L10.4 22.4 L13.1 21.1 L9.85 14.3 L17.2 14.0 Z" />
      </svg>
      {/* Hand glyph — shown on hover over links/buttons */}
      <svg className="mmrl-hand" width="22" height="24" viewBox="0 0 22 24" fill="none">
        <path d="M8 13 L8 4.2 C8 3.3 8.7 2.6 9.6 2.6 C10.5 2.6 11.2 3.3 11.2 4.2 L11.2 11 L11.2 7.4 C11.2 6.5 11.9 5.8 12.8 5.8 C13.7 5.8 14.4 6.5 14.4 7.4 L14.4 11 L14.4 8.6 C14.4 7.7 15.1 7 16 7 C16.9 7 17.6 7.7 17.6 8.6 L17.6 11.6 L17.6 9.6 C17.6 8.8 18.25 8.2 19.05 8.2 C19.85 8.2 20.5 8.8 20.5 9.6 L20.5 16 C20.5 19.6 17.6 22 13.8 22 L11.5 22 C9 22 7 20.6 5.6 18.4 L2.7 13.6 C2.2 12.7 2.5 11.6 3.4 11.1 C4.2 10.7 5.2 10.95 5.7 11.7 L8 14.5 Z"
              className="mmrl-hand-fill" />
        <path d="M8 13 L8 4.2 C8 3.3 8.7 2.6 9.6 2.6 C10.5 2.6 11.2 3.3 11.2 4.2 L11.2 11 M11.2 11 L11.2 7.4 C11.2 6.5 11.9 5.8 12.8 5.8 C13.7 5.8 14.4 6.5 14.4 7.4 L14.4 11 M14.4 11 L14.4 8.6 C14.4 7.7 15.1 7 16 7 C16.9 7 17.6 7.7 17.6 8.6 L17.6 11.6 M17.6 11.6 L17.6 9.6 C17.6 8.8 18.25 8.2 19.05 8.2 C19.85 8.2 20.5 8.8 20.5 9.6 L20.5 16 C20.5 19.6 17.6 22 13.8 22 L11.5 22 C9 22 7 20.6 5.6 18.4 L2.7 13.6 C2.2 12.7 2.5 11.6 3.4 11.1 C4.2 10.7 5.2 10.95 5.7 11.7 L8 14.5"
              className="mmrl-hand-stroke" />
      </svg>
    </div>
  );
};

window.CustomCursor = CustomCursor;
