const { useState, useEffect, useRef } = React;

// ── Shared hook ───────────────────────────────────────────────
function useReveal() {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const obs = new IntersectionObserver(
      ([entry]) => { if (entry.isIntersecting) el.classList.add('revealed'); },
      { threshold: 0.05, rootMargin: '0px 0px -40px 0px' }
    );
    obs.observe(el);
    return () => obs.disconnect();
  }, []);
  return ref;
}

// ── Icons ─────────────────────────────────────────────────────
function IconArrow() {
  return <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"><line x1="7" y1="17" x2="17" y2="7"/><polyline points="7 7 17 7 17 17"/></svg>;
}
function IconPlay() {
  return <svg width="13" height="13" viewBox="0 0 24 24" fill="currentColor"><polygon points="5,3 19,12 5,21"/></svg>;
}
function IconSpotify() {
  return <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor"><path d="M12 0C5.4 0 0 5.4 0 12s5.4 12 12 12 12-5.4 12-12S18.66 0 12 0zm5.521 17.34c-.24.359-.66.48-1.021.24-2.82-1.74-6.36-2.101-10.561-1.141-.418.122-.779-.179-.899-.539-.12-.421.18-.78.54-.9 4.56-1.021 8.52-.6 11.64 1.32.42.18.479.659.301 1.02zm1.44-3.3c-.301.42-.841.6-1.262.3-3.239-1.98-8.159-2.58-11.939-1.38-.479.12-1.02-.12-1.14-.6-.12-.48.12-1.021.6-1.141C9.6 9.9 15 10.561 18.72 12.84c.361.181.54.78.241 1.2zm.12-3.36C15.24 8.4 8.82 8.16 5.16 9.301c-.6.179-1.2-.181-1.38-.721-.18-.601.18-1.2.72-1.381 4.26-1.26 11.28-1.02 15.721 1.621.539.3.719 1.02.419 1.56-.299.421-1.02.599-1.559.3z"/></svg>;
}
function IconMusic() {
  return <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M9 18V5l12-2v13"/><circle cx="6" cy="18" r="3"/><circle cx="18" cy="16" r="3"/></svg>;
}
function IconCalendar() {
  return <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>;
}

// ── Navbar ────────────────────────────────────────────────────
function Navbar() {
  const [scrolled, setScrolled] = useState(false);
  const [mobileOpen, setMobileOpen] = useState(false);
  const { user, openModal } = useAuth();

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 40);
    window.addEventListener('scroll', onScroll);
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const links = [
    { label: 'About', href: '#about' },
    { label: 'Music', href: '#music' },
    { label: 'Shows', href: '#shows' },
    { label: 'Contact', href: '#contact' },
  ];

  return (
    <nav style={{
      position: 'fixed', top: 16, left: 0, right: 0, zIndex: 50,
      padding: '0 24px', display: 'grid', gridTemplateColumns: '1fr auto 1fr', alignItems: 'center', gap: 12,
    }}>
      {/* Logo — home button */}
      <a href="#hero" style={{ display: 'flex', alignItems: 'center', textDecoration: 'none', justifyContent: 'flex-start' }}>
        <img
          src="uploads/AiFonDeW Owl.png"
          alt="FonDeW"
          style={{ height: 44, width: 44, objectFit: 'cover', objectPosition: 'top', borderRadius: '50%', border: '1px solid rgba(255,255,255,0.15)' }}
        />
      </a>

      {/* Center nav pill — truly centered via grid */}
      <div className="hidden-mobile">
        <div className="liquid-glass" style={{ borderRadius: 9999, padding: '5px 6px', display: 'flex', alignItems: 'center', gap: 2 }}>
          {links.map(l => (
            <a key={l.label} href={l.href} style={{
              padding: '6px 14px', borderRadius: 9999,
              fontSize: '0.82rem', fontWeight: 500, color: 'rgba(255,255,255,0.85)',
              textDecoration: 'none', fontFamily: "'Barlow', sans-serif",
              transition: 'background 0.2s, color 0.2s',
              whiteSpace: 'nowrap',
            }}
              onMouseEnter={e => e.currentTarget.style.background = 'rgba(255,255,255,0.08)'}
              onMouseLeave={e => e.currentTarget.style.background = 'transparent'}
            >{l.label}</a>
          ))}
        </div>
      </div>

      {/* Right: Sign In */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, justifyContent: 'flex-end' }}>
        <a href="#" onClick={e => { e.preventDefault(); openModal(user ? 'wallet' : 'signin'); }} className="liquid-glass-strong" style={{
          borderRadius: 9999, padding: '7px 18px',
          fontSize: '0.8rem', fontWeight: 500, color: user ? 'var(--accent)' : '#fff',
          textDecoration: 'none', whiteSpace: 'nowrap', cursor: 'pointer',
        }}>{user ? `🦉 ${useAuth().userName?.split(' ')[0] || ''}` : 'Sign In'}</a>
        {/* Mobile hamburger */}
        <button onClick={() => setMobileOpen(!mobileOpen)}
          style={{ background: 'none', border: 'none', color: '#fff', cursor: 'pointer', padding: 4, marginLeft: 4 }}
          className="show-mobile"
        >
          <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
            {mobileOpen
              ? <><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></>
              : <><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="18" x2="21" y2="18"/></>
            }
          </svg>
        </button>
      </div>

      {/* Mobile menu */}
      {mobileOpen && (
        <div className="liquid-glass" style={{
          position: 'absolute', top: '100%', left: 16, right: 16, marginTop: 8,
          borderRadius: 16, padding: '12px 8px', display: 'flex', flexDirection: 'column', gap: 2,
        }}>
          {links.map(l => (
            <a key={l.label} href={l.href} onClick={() => setMobileOpen(false)}
              style={{
                padding: '10px 16px', borderRadius: 10, fontSize: '0.9rem',
                fontWeight: 400, color: 'rgba(255,255,255,0.85)', textDecoration: 'none',
              }}>
              {l.label}
            </a>
          ))}
        </div>
      )}

      <style>{`
        @media (max-width: 767px) { .hidden-mobile { display: none !important; } }
        @media (min-width: 768px) { .show-mobile { display: none !important; } }
      `}</style>
    </nav>
  );
}

Object.assign(window, { Navbar, useReveal, IconArrow, IconPlay, IconSpotify, IconMusic, IconCalendar });
