/* global React */
const { useState, useEffect, useRef } = React;

// ============ Hook: scroll reveal ============
function useReveal() {
  useEffect(() => {
    const els = document.querySelectorAll('.reveal, .reveal-left, .reveal-right, .reveal-scale, .reveal-mask, .reveal-line, .reveal-words');
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) {
          e.target.classList.add('in');
          io.unobserve(e.target);
        }
      });
    }, { threshold: 0.12, rootMargin: '0px 0px -60px 0px' });
    els.forEach(el => io.observe(el));
    return () => io.disconnect();
  });
}

// ============ Hook: count up ============
function useCountUp(target, trigger, duration = 1800) {
  const [val, setVal] = useState(0);
  useEffect(() => {
    if (!trigger) return;
    let start;
    let raf;
    const step = (t) => {
      if (!start) start = t;
      const p = Math.min((t - start) / duration, 1);
      const eased = 1 - Math.pow(1 - p, 3);
      setVal(Math.floor(eased * target));
      if (p < 1) raf = requestAnimationFrame(step);
      else setVal(target);
    };
    raf = requestAnimationFrame(step);
    return () => cancelAnimationFrame(raf);
  }, [trigger, target, duration]);
  return val;
}

// ============ Hook: scroll Y ============
function useScrollY() {
  const [y, setY] = useState(0);
  useEffect(() => {
    const onScroll = () => setY(window.scrollY);
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  return y;
}

// ============ Nav ============
function Nav() {
  const y = useScrollY();
  const scrolled = y > 40;
  return (
    <nav style={{
      position: 'fixed', top: 0, left: 0, right: 0, zIndex: 100,
      transition: 'all 280ms ease',
      background: scrolled ? 'rgba(10, 22, 40, 0.92)' : 'transparent',
      backdropFilter: scrolled ? 'blur(14px)' : 'none',
      borderBottom: scrolled ? '1px solid rgba(212, 168, 87, 0.15)' : '1px solid transparent',
    }}>
      <div className="container" style={{
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        height: scrolled ? 68 : 88, transition: 'height 280ms ease',
      }}>
        <a href="#top" style={{
          display: 'flex', alignItems: 'center', gap: 12,
          textDecoration: 'none', color: 'var(--paper)',
        }}>
          <svg width="46" height="46" viewBox="0 0 80 80" style={{ flexShrink: 0 }}>
            <defs>
              <linearGradient id="goldGrad" x1="0%" y1="0%" x2="100%" y2="100%">
                <stop offset="0%" stopColor="#f4dc9a"/>
                <stop offset="50%" stopColor="#d4a857"/>
                <stop offset="100%" stopColor="#8a6420"/>
              </linearGradient>
              <linearGradient id="goldRing" x1="0%" y1="0%" x2="0%" y2="100%">
                <stop offset="0%" stopColor="#e8c477"/>
                <stop offset="100%" stopColor="#9c7530"/>
              </linearGradient>
            </defs>
            {/* Premium double ring frame */}
            <circle cx="40" cy="40" r="38" fill="none" stroke="url(#goldRing)" strokeWidth="0.8"/>
            <circle cx="40" cy="40" r="35" fill="none" stroke="url(#goldRing)" strokeWidth="0.4" opacity="0.55"/>

            {/* Interlocked PG via serif glyphs */}
            {/* G (drawn first, sits behind) */}
            <text x="46" y="56" textAnchor="middle"
              fontFamily="'Fraunces','Cormorant Garamond','Times New Roman',serif"
              fontWeight="500" fontSize="48" fill="url(#goldGrad)" letterSpacing="-2">G</text>
            {/* P (drawn on top, slightly overlapping G) */}
            <text x="32" y="56" textAnchor="middle"
              fontFamily="'Fraunces','Cormorant Garamond','Times New Roman',serif"
              fontWeight="600" fontSize="48" fill="url(#goldGrad)" letterSpacing="-2">P</text>
          </svg>
          <div style={{ display: 'flex', flexDirection: 'column', lineHeight: 1.1 }}>
            <span className="display" style={{ fontSize: 22, fontWeight: 500, letterSpacing: '-0.02em' }}>Prime Global</span>
            <span className="mono" style={{ fontSize: 10, letterSpacing: '0.24em', textTransform: 'uppercase', color: 'var(--gold)', fontWeight: 500 }}>Trade — Est. 2014</span>
          </div>
        </a>
        <div style={{ display: 'flex', alignItems: 'center', gap: 36 }} className="nav-links">
          {[
            { label: 'Services', href: '#services' },
            { label: 'Capabilities', href: '#capabilities' },
            { label: 'Why Us', href: '#why-us' },
            { label: 'Contact', href: '#contact' },
          ].map(({label: l, href}) => (
            <a key={l} href={href} style={{
              color: 'var(--paper)', textDecoration: 'none', fontSize: 13,
              fontWeight: 500, letterSpacing: '0.04em', opacity: 0.85,
              transition: 'opacity 200ms ease',
            }} onMouseEnter={e => e.currentTarget.style.opacity = '1'}
              onMouseLeave={e => e.currentTarget.style.opacity = '0.85'}>
              {l}
            </a>
          ))}
          
          <a href="#contact" className="btn btn-primary" style={{ padding: '12px 22px', fontSize: 13 }}>
            Get a Quote <span className="arrow">→</span>
          </a>
        </div>
      </div>
    </nav>
  );
}

Object.assign(window, { useReveal, useCountUp, useScrollY, Nav });
