// Shared utilities + SVG placeholder art components

const { useState, useEffect, useRef, useMemo, useCallback } = React;

// Hook: reveal on scroll
function useReveal() {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current; if (!el) return;
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => { if (e.isIntersecting) { e.target.classList.add('in'); io.unobserve(e.target); } });
    }, { threshold: 0.15, rootMargin: '0px 0px -80px 0px' });
    io.observe(el);
    return () => io.disconnect();
  }, []);
  return ref;
}

// Hook: counter (animated)
function useCountUp(target, { duration = 1600, when = true } = {}) {
  const [v, setV] = useState(0);
  const started = useRef(false);
  useEffect(() => {
    if (!when || started.current) return;
    started.current = true;
    const start = performance.now();
    const tick = (t) => {
      const p = Math.min(1, (t - start) / duration);
      const eased = 1 - Math.pow(1 - p, 3);
      setV(target * eased);
      if (p < 1) requestAnimationFrame(tick);
    };
    requestAnimationFrame(tick);
  }, [when, target, duration]);
  return v;
}

// Formats
const fmt = {
  money: (n) => `$${n.toFixed(0)}B`,
  int: (n) => Math.round(n).toLocaleString(),
};

// Reveal wrapper
function Reveal({ children, delay = 0, as = 'div', className = '', ...rest }) {
  const r = useReveal();
  const Tag = as;
  const dClass = delay ? ` r-${delay}` : '';
  return <Tag ref={r} className={`reveal${dClass} ${className}`} {...rest}>{children}</Tag>;
}

// Logo mark
function LogoMark({ size = 22, color = 'var(--ink)' }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <path d="M3 21 L3 11 L12 3 L21 11 L21 21 Z" stroke={color} strokeWidth="1.6" strokeLinejoin="round" fill="none" />
      <path d="M8 21 L8 14 L16 14 L16 21" stroke={color} strokeWidth="1.6" strokeLinejoin="round" fill="none" />
      <circle cx="12" cy="9" r="1.3" fill="var(--accent)" />
    </svg>
  );
}

// Arrow
const ArrowRight = ({ size = 16 }) => (
  <svg width={size} height={size} viewBox="0 0 16 16" fill="none" aria-hidden>
    <path d="M3 8 H13 M9 4 L13 8 L9 12" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" />
  </svg>
);

// Placeholder art: Oklahoma prairie aerial (used for hero / radar befores)
function PrairieAerialSVG({ variant = 'day', className = '' }) {
  const night = variant === 'night';
  return (
    <svg viewBox="0 0 1600 900" preserveAspectRatio="xMidYMid slice" className={className} aria-hidden>
      <defs>
        <linearGradient id={`sky-${variant}`} x1="0" y1="0" x2="0" y2="1">
          {night ? (<>
            <stop offset="0" stopColor="#05111d" />
            <stop offset=".7" stopColor="#0a2231" />
            <stop offset="1" stopColor="#0f2e33" />
          </>) : (<>
            <stop offset="0" stopColor="#c9e3d9" />
            <stop offset=".55" stopColor="#e9efd8" />
            <stop offset="1" stopColor="#d8cfa3" />
          </>)}
        </linearGradient>
        <linearGradient id={`land-${variant}`} x1="0" y1="0" x2="0" y2="1">
          {night ? (<>
            <stop offset="0" stopColor="#0b2216" />
            <stop offset="1" stopColor="#061409" />
          </>) : (<>
            <stop offset="0" stopColor="#6d8f4f" />
            <stop offset=".5" stopColor="#8ba85f" />
            <stop offset="1" stopColor="#c6b87a" />
          </>)}
        </linearGradient>
        <radialGradient id={`sun-${variant}`} cx=".5" cy=".5" r=".5">
          {night ? (<>
            <stop offset="0" stopColor="#f6f0d9" stopOpacity="1" />
            <stop offset=".5" stopColor="#f6f0d9" stopOpacity=".3" />
            <stop offset="1" stopColor="#f6f0d9" stopOpacity="0" />
          </>) : (<>
            <stop offset="0" stopColor="#fff4c7" stopOpacity="1" />
            <stop offset=".5" stopColor="#fff4c7" stopOpacity=".4" />
            <stop offset="1" stopColor="#fff4c7" stopOpacity="0" />
          </>)}
        </radialGradient>
      </defs>
      <rect width="1600" height="520" fill={`url(#sky-${variant})`} />
      <rect y="520" width="1600" height="380" fill={`url(#land-${variant})`} />
      {/* Sun / moon */}
      <circle cx="1200" cy="180" r="180" fill={`url(#sun-${variant})`} />
      <circle cx="1200" cy="180" r={night ? 48 : 40} fill={night ? '#f6f0d9' : '#fff5c4'} />
      {/* stars on night */}
      {night && Array.from({length: 40}).map((_, i) => (
        <circle key={i} cx={Math.random()*1600} cy={Math.random()*380} r={Math.random()*1.2+0.3} fill="#ffffff" opacity={Math.random()*.7 + .2} />
      ))}
      {/* Distant horizon field rows */}
      {Array.from({length: 14}).map((_, i) => {
        const y = 540 + i * 22;
        return <path key={i} d={`M0 ${y} Q 400 ${y - 8 + i}, 800 ${y} T 1600 ${y}`} stroke={night ? 'rgba(255,255,255,.04)' : 'rgba(10,24,18,.08)'} strokeWidth="1" fill="none" />;
      })}
      {/* Isolated trees */}
      {[[180,660],[320,700],[900,640],[1300,720],[1480,680]].map(([x,y], i) => (
        <g key={i}>
          <ellipse cx={x} cy={y} rx={22} ry={28} fill={night ? '#071a10' : '#415a2a'} opacity=".9" />
          <rect x={x-1} y={y+22} width="3" height="14" fill={night ? '#061209' : '#2a3a1a'} />
        </g>
      ))}
      {/* Road */}
      <path d="M0 820 Q 400 780, 800 800 T 1600 780" stroke={night ? '#1a2a22' : '#e6dec6'} strokeWidth="18" fill="none" />
      <path d="M0 820 Q 400 780, 800 800 T 1600 780" stroke={night ? '#2d4035' : '#a89668'} strokeWidth="2" strokeDasharray="18 22" fill="none" />
    </svg>
  );
}

// Placeholder: campus aerial with berms — used for after state and hero
function CampusAerialSVG({ variant = 'day', className = '' }) {
  const night = variant === 'night';
  return (
    <svg viewBox="0 0 1600 900" preserveAspectRatio="xMidYMid slice" className={className} aria-hidden>
      <defs>
        <linearGradient id={`csky-${variant}`} x1="0" y1="0" x2="0" y2="1">
          {night ? (<>
            <stop offset="0" stopColor="#061424" />
            <stop offset="1" stopColor="#0b2a2e" />
          </>) : (<>
            <stop offset="0" stopColor="#bcd8e2" />
            <stop offset="1" stopColor="#e0e6c8" />
          </>)}
        </linearGradient>
        <linearGradient id={`cland-${variant}`} x1="0" y1="0" x2="0" y2="1">
          {night ? (<>
            <stop offset="0" stopColor="#0a2316" />
            <stop offset="1" stopColor="#030d07" />
          </>) : (<>
            <stop offset="0" stopColor="#6a8c4c" />
            <stop offset="1" stopColor="#b8ac70" />
          </>)}
        </linearGradient>
      </defs>
      <rect width="1600" height="380" fill={`url(#csky-${variant})`} />
      <rect y="380" width="1600" height="520" fill={`url(#cland-${variant})`} />
      {/* Berm ring */}
      <ellipse cx="800" cy="620" rx="720" ry="230" fill={night ? '#0c2818' : '#7a9955'} opacity=".9" />
      <ellipse cx="800" cy="620" rx="620" ry="180" fill={night ? '#0a2013' : '#6d8b4a'} />
      {/* Trees around berm */}
      {Array.from({length: 36}).map((_, i) => {
        const a = (i / 36) * Math.PI * 2;
        const rx = 680, ry = 210;
        const cx = 800 + Math.cos(a) * rx;
        const cy = 620 + Math.sin(a) * ry;
        return <circle key={i} cx={cx} cy={cy} r={14 + (i%3)*3} fill={night ? '#061309' : '#3a5422'} opacity=".95" />;
      })}
      {/* Campus buildings (low profile) */}
      {[
        [620, 580, 180, 90], [820, 590, 220, 110], [720, 680, 160, 80], [860, 680, 120, 60]
      ].map(([x,y,w,h], i) => (
        <g key={i}>
          <rect x={x} y={y} width={w} height={h} rx="4" fill={night ? '#0f2a1c' : '#eae4d2'} stroke={night ? '#1d3a2a' : '#b7ac82'} strokeWidth="1.4" />
          {night && (
            <>
              <rect x={x+6} y={y+6} width={w-12} height="3" fill="#f6c06a" opacity=".6" />
              <circle cx={x+w-14} cy={y+h-14} r="2" fill="#f6c06a" opacity=".7" />
            </>
          )}
          {!night && Array.from({length: Math.floor(w/22)}).map((_, j) => (
            <rect key={j} x={x + 8 + j*22} y={y+6} width={10} height={h-12} fill="rgba(255,255,255,.2)" />
          ))}
        </g>
      ))}
      {/* Access road */}
      <path d={`M 100 860 Q 500 780 800 700`} stroke={night ? '#1a2e24' : '#ccbd89'} strokeWidth="10" fill="none" />
      {/* Accent glow */}
      {!night && <circle cx="820" cy="600" r="260" fill="none" stroke="rgba(20,32,27,.18)" strokeWidth="1" strokeDasharray="4 6" />}
    </svg>
  );
}

// Icon set
const Icons = {
  Drop: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <path d="M12 3 C 7 10, 5 14, 5 17 a7 7 0 0 0 14 0 c 0 -3 -2 -7 -7 -14 Z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round" />
    </svg>
  ),
  Leaf: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <path d="M20 4 C 10 4, 4 10, 4 20 C 14 20, 20 14, 20 4 Z M6 18 L 16 8" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round" />
    </svg>
  ),
  Wave: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <path d="M3 9 Q 6 5, 9 9 T 15 9 T 21 9 M3 15 Q 6 11, 9 15 T 15 15 T 21 15" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" />
    </svg>
  ),
  Bolt: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <path d="M13 2 L4 14 H11 L9 22 L20 10 H13 Z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round" />
    </svg>
  ),
  Shield: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <path d="M12 3 L20 6 V12 C 20 17, 16 20, 12 22 C 8 20, 4 17, 4 12 V6 Z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round" />
    </svg>
  ),
  Truck: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <rect x="2" y="7" width="12" height="9" stroke="currentColor" strokeWidth="1.4" />
      <path d="M14 10 H 19 L 22 14 V16 H14 Z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round" />
      <circle cx="7" cy="18" r="2" stroke="currentColor" strokeWidth="1.4" />
      <circle cx="17" cy="18" r="2" stroke="currentColor" strokeWidth="1.4" />
    </svg>
  ),
  Briefcase: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <rect x="3" y="7" width="18" height="13" rx="2" stroke="currentColor" strokeWidth="1.4" />
      <path d="M9 7 V5 a2 2 0 0 1 2 -2 h2 a2 2 0 0 1 2 2 V7" stroke="currentColor" strokeWidth="1.4" />
    </svg>
  ),
  School: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <path d="M4 10 L12 5 L20 10 V20 H4 Z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round" />
      <path d="M10 20 V14 H14 V20" stroke="currentColor" strokeWidth="1.4" />
    </svg>
  ),
  Home: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <path d="M4 11 L12 4 L20 11 V20 H4 Z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round" />
    </svg>
  ),
  Speaker: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <path d="M4 10 H8 L13 6 V18 L8 14 H4 Z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round" />
      <path d="M16 9 Q 19 12, 16 15 M18 6 Q 23 12, 18 18" stroke="currentColor" strokeWidth="1.4" />
    </svg>
  ),
  Moon: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <path d="M20 14 A 8 8 0 1 1 10 4 A 6 6 0 0 0 20 14 Z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round" />
    </svg>
  ),
  Sun: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <circle cx="12" cy="12" r="4" stroke="currentColor" strokeWidth="1.4" />
      {[0,45,90,135,180,225,270,315].map(a => {
        const r1 = 6.5, r2 = 9.5;
        const x1 = 12 + Math.cos(a*Math.PI/180)*r1, y1 = 12 + Math.sin(a*Math.PI/180)*r1;
        const x2 = 12 + Math.cos(a*Math.PI/180)*r2, y2 = 12 + Math.sin(a*Math.PI/180)*r2;
        return <line key={a} x1={x1} y1={y1} x2={x2} y2={y2} stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" />;
      })}
    </svg>
  ),
  Chat: ({ size = 24 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden>
      <path d="M4 6 h16 v11 h-7 l-4 4 v-4 H4 Z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round" />
    </svg>
  ),
  Plus: ({ size = 16 }) => (
    <svg width={size} height={size} viewBox="0 0 16 16" fill="none" aria-hidden>
      <path d="M8 3 V13 M3 8 H13" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
    </svg>
  ),
};

Object.assign(window, {
  useReveal, useCountUp, Reveal, LogoMark, ArrowRight, Icons,
  PrairieAerialSVG, CampusAerialSVG, fmt,
});
