/* Main site navigation — used on index, indexv2, contact, code-inwisselen,
   the 3 legal pages under /overig, and the /check/<slug>/ landing pages.

   Props:
     heroStyle  — 'dark' (default) or 'light'. 'light' inverts colours for hero-light pages.
     isMobile   — boolean from useWindowWidth(); toggles desktop links vs hamburger.
     activePage — 'contact' | 'code-inwisselen' | 'home' | a CHECKS_MENU slug like 'rdw'.
                  Highlights the matching link.
     basePath   — prepend for pages in subdirectories. '' for root, '../' for /overig/*,
                  '../../' for /check/<slug>/. Defaults to ''.

   Depends on: Stars (components/Stars.jsx), useWindowWidth (js/window-width.js). */
const CHECKS_MENU = [
  { slug: 'rdw',              label: 'RDW Check',           desc: 'Officiële RDW gegevens' },
  { slug: 'kilometer',        label: 'Kilometer Check',     desc: 'Tellerstand & afwijkingen' },
  { slug: 'nap',              label: 'NAP Check',           desc: 'Nationale Auto Pas verificatie' },
  { slug: 'schade',           label: 'Schade Check',        desc: 'Schade & total-loss historie' },
  { slug: 'import',           label: 'Import Check',        desc: 'Geïmporteerde voertuigen' },
  { slug: 'marktplaats',      label: 'Marktplaats Check',   desc: 'Veilig kopen van particulier' },
  { slug: 'motoren',          label: 'Motor Check',         desc: 'Kentekencheck voor motoren' },
  { slug: 'campers',          label: 'Camper Check',        desc: 'Camper & motorhome' },
  { slug: 'bedrijfswagens',   label: 'Bedrijfswagen Check', desc: 'Bestelbus & grijs kenteken' },
  { slug: 'elektrische-auto', label: 'Elektrische Auto',    desc: 'EV — accu, range, label' },
  { slug: 'vrachtwagens',     label: 'Vrachtwagen Check',   desc: 'N2/N3 & zware bedrijfswagens' },
];

function Nav({ heroStyle = 'dark', isMobile = false, activePage, basePath = '' }) {
  const { useState, useEffect, useRef } = React;
  const dark = heroStyle !== 'light';
  const navW = useWindowWidth();
  const isTiny = navW < 350;
  const showBrandWordmark = !isMobile || navW >= 520;
  const [dropOpen, setDropOpen] = useState(false);
  const [menuOpen, setMenuOpen] = useState(false);
  const dropRef = useRef(null);

  useEffect(() => {
    function outside(e) {
      if (dropRef.current && !dropRef.current.contains(e.target)) setDropOpen(false);
    }
    document.addEventListener('mousedown', outside);
    return () => document.removeEventListener('mousedown', outside);
  }, []);

  const lc = dark ? 'rgba(255,255,255,0.82)' : '#0F1F5C';
  const navLinkStyle = {
    color: lc, fontWeight: 500, fontSize: 14, textDecoration: 'none',
    padding: '6px 12px', borderRadius: 8, transition: 'background .15s',
  };
  const activeLinkStyle = { ...navLinkStyle, color: '#FFD12E', fontWeight: 700 };
  const isActive = (page) => activePage === page;

  const indexHref       = basePath || '/';
  const codeHref        = basePath + 'code-inwisselen';
  const contactHref     = basePath + 'contact';

  return (
    <nav id="main-nav" style={{
      background: dark ? 'rgba(10,22,64,0.95)' : 'white',
      borderBottom: `1px solid ${dark ? 'rgba(255,255,255,0.06)' : '#e8eaf5'}`,
      position: 'sticky', top: 0, zIndex: 100, backdropFilter: 'blur(10px)',
    }}>
      <div style={{
        maxWidth: 1200, margin: '0 auto',
        padding: isMobile ? (isTiny ? '0 10px' : '0 16px') : '0 24px',
        height: 64, display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: isMobile ? 10 : 8,
      }}>
        {/* Logo */}
        <a href={indexHref} style={{
          display: 'flex', alignItems: 'center', gap: isMobile ? 6 : 8,
          textDecoration: 'none', minWidth: 0, overflow: 'hidden', flex: '1 1 auto',
        }}>
          <BrandBadge size={isMobile ? (isTiny ? 24 : 28) : 30} basePath={basePath} variant={dark ? 'white' : 'blue'}/>
          {showBrandWordmark && (
            <BrandWordmark size={15} color={dark ? 'white' : '#0F1F5C'}/>
          )}
        </a>

        {/* Desktop nav */}
        {!isMobile && (
          <div style={{ display: 'flex', alignItems: 'center', gap: 4 }}>
            {/* Phone */}
            <a href="tel:+31854019836" style={{
              color: '#FFD12E', fontWeight: 700, fontSize: 14, textDecoration: 'none',
              padding: '6px 12px', borderRadius: 8, display: 'flex', alignItems: 'center', gap: 6,
              transition: 'background .15s', whiteSpace: 'nowrap',
            }}
               onMouseEnter={e => e.currentTarget.style.background = 'rgba(255,255,255,0.08)'}
               onMouseLeave={e => e.currentTarget.style.background = 'transparent'}>
              <svg width="14" height="14" viewBox="0 0 20 20" fill="none" stroke="#FFD12E" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
                <path d="M3 4h3.5l1.5 4-2 1.5c1 2 2.5 3.5 4.5 4.5L12 12l4 1.5V17c0 .6-.4 1-1 1C6 18 2 12 2 5c0-.6.4-1 1-1z"/>
              </svg>
              +31854019836
            </a>
            <div style={{ width: 1, height: 20, background: 'rgba(255,255,255,0.15)', margin: '0 4px' }}/>

            {/* Checks dropdown */}
            <div ref={dropRef} style={{ position: 'relative' }}>
              <button onClick={() => setDropOpen(o => !o)} style={{
                background: 'transparent', border: 'none', cursor: 'pointer',
                color: lc, fontWeight: 500, fontSize: 14,
                padding: '6px 12px', borderRadius: 8, fontFamily: "'Plus Jakarta Sans',sans-serif",
                display: 'flex', alignItems: 'center', gap: 6, transition: 'background .15s',
              }}
                      onMouseEnter={e => e.currentTarget.style.background = 'rgba(255,255,255,0.08)'}
                      onMouseLeave={e => e.currentTarget.style.background = 'transparent'}>
                Checks
                <svg width="14" height="14" viewBox="0 0 14 14" fill="none"
                     style={{ transition: 'transform .2s', transform: dropOpen ? 'rotate(180deg)' : 'none' }}>
                  <path d="M3 5l4 4 4-4" stroke={lc} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/>
                </svg>
              </button>
              {dropOpen && (
                <div style={{
                  position: 'absolute', top: 'calc(100% + 8px)', left: 0,
                  background: 'white', borderRadius: 14, border: '1px solid #e8eaf5',
                  boxShadow: '0 16px 48px rgba(15,31,92,0.15)', width: 280,
                  maxHeight: 'min(70vh, 560px)', overflowY: 'auto', zIndex: 200,
                }}>
                  {CHECKS_MENU.map((item, i) => {
                    const active = isActive(item.slug);
                    return (
                      <a key={item.slug} href={basePath + 'check/' + item.slug + '/'} style={{
                        display: 'flex', flexDirection: 'column', padding: '12px 16px',
                        textDecoration: 'none',
                        background: active ? '#f5f7ff' : 'white',
                        borderBottom: i < CHECKS_MENU.length - 1 ? '1px solid #f0f2f8' : 'none',
                        transition: 'background .1s',
                      }}
                         onMouseEnter={e => e.currentTarget.style.background = '#f8f9fd'}
                         onMouseLeave={e => e.currentTarget.style.background = active ? '#f5f7ff' : 'white'}>
                        <span style={{ fontWeight: 700, fontSize: 13, color: active ? '#003CAA' : '#0F1F5C' }}>{item.label}</span>
                        <span style={{ fontSize: 12, color: '#8a96b8', marginTop: 2 }}>{item.desc}</span>
                      </a>
                    );
                  })}
                </div>
              )}
            </div>

            <a href={codeHref}
               style={isActive('code-inwisselen') ? activeLinkStyle : navLinkStyle}
               onMouseEnter={e => { if (!isActive('code-inwisselen')) e.currentTarget.style.background = 'rgba(255,255,255,0.08)'; }}
               onMouseLeave={e => { if (!isActive('code-inwisselen')) e.currentTarget.style.background = 'transparent'; }}>
              Code inwisselen
            </a>
            <a href={contactHref}
               style={isActive('contact') ? activeLinkStyle : navLinkStyle}
               onMouseEnter={e => { if (!isActive('contact')) e.currentTarget.style.background = 'rgba(255,255,255,0.08)'; }}
               onMouseLeave={e => { if (!isActive('contact')) e.currentTarget.style.background = 'transparent'; }}>
              Contact
            </a>
            <div style={{ width: 1, height: 20, background: 'rgba(255,255,255,0.15)', margin: '0 8px' }}/>
            <div style={{ display: 'flex', alignItems: 'center', gap: 6, background: 'rgba(255,255,255,0.08)', borderRadius: 20, padding: '4px 12px 4px 8px' }}>
              <Stars/><span style={{ color: lc, fontSize: 13, fontWeight: 600 }}>4,7/5</span>
            </div>
          </div>
        )}

        {/* Mobile hamburger */}
        {isMobile && (
          <div style={{ display: 'flex', alignItems: 'center', gap: isTiny ? 8 : 12, flex: '0 0 auto' }}>
            <a href="tel:+31854019836" style={{
              color: '#FFD12E', display: 'flex', alignItems: 'center', gap: 6,
              textDecoration: 'none', fontWeight: 700, fontSize: isTiny ? 12 : 13,
              whiteSpace: 'nowrap', flex: '0 0 auto', lineHeight: 1,
            }}>
              <svg width={isTiny ? 16 : 18} height={isTiny ? 16 : 18} viewBox="0 0 20 20" fill="none" stroke="#FFD12E" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" style={{ flexShrink: 0 }}>
                <path d="M3 4h3.5l1.5 4-2 1.5c1 2 2.5 3.5 4.5 4.5L12 12l4 1.5V17c0 .6-.4 1-1 1C6 18 2 12 2 5c0-.6.4-1 1-1z"/>
              </svg>
              <span>+31854019836</span>
            </a>
            <button onClick={() => setMenuOpen(o => !o)} style={{
              background: 'transparent', border: 'none', cursor: 'pointer',
              padding: 6, display: 'flex', flexDirection: 'column', gap: 5, alignItems: 'center',
            }}>
              {[0, 1, 2].map(i => (
                <div key={i} style={{
                  width: 22, height: 2,
                  background: dark ? 'white' : '#0F1F5C',
                  borderRadius: 2, transition: 'all .2s',
                  transform:
                    menuOpen && i === 0 ? 'rotate(45deg) translate(5px,5px)' :
                    menuOpen && i === 1 ? 'scaleX(0)' :
                    menuOpen && i === 2 ? 'rotate(-45deg) translate(5px,-5px)' : 'none',
                }}/>
              ))}
            </button>
          </div>
        )}
      </div>

      {/* Mobile menu dropdown */}
      {isMobile && menuOpen && (
        <div style={{
          background: dark ? 'rgba(10,22,64,0.99)' : 'white',
          borderTop: `1px solid ${dark ? 'rgba(255,255,255,0.06)' : '#e8eaf5'}`,
          padding: '8px 24px 16px',
        }}>
          <a href={indexHref} style={{
            display: 'flex', alignItems: 'center', gap: 10,
            textDecoration: 'none',
            padding: '12px 0',
            borderBottom: `1px solid ${dark ? 'rgba(255,255,255,0.06)' : '#f0f2f8'}`,
          }}>
            <BrandBadge size={26} basePath={basePath} variant={dark ? 'white' : 'blue'}/>
            <BrandWordmark size={14} color={dark ? 'white' : '#0F1F5C'}/>
          </a>
          <a href="tel:+31854019836" style={{
            display: 'flex', alignItems: 'center', gap: 8,
            color: '#FFD12E', fontWeight: 700, fontSize: 14, textDecoration: 'none',
            padding: '12px 0', borderBottom: `1px solid ${dark ? 'rgba(255,255,255,0.06)' : '#f0f2f8'}`,
          }}>
            <svg width="14" height="14" viewBox="0 0 20 20" fill="none" stroke="#FFD12E" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
              <path d="M3 4h3.5l1.5 4-2 1.5c1 2 2.5 3.5 4.5 4.5L12 12l4 1.5V17c0 .6-.4 1-1 1C6 18 2 12 2 5c0-.6.4-1 1-1z"/>
            </svg>
            +31854019836
          </a>
          {[
            { l: 'Kentekencheck', h: indexHref, page: 'home' },
            ...CHECKS_MENU.map(item => ({
              l: item.label,
              h: basePath + 'check/' + item.slug + '/',
              page: item.slug,
            })),
            { l: 'Code inwisselen', h: codeHref,    page: 'code-inwisselen' },
            { l: 'Contact',         h: contactHref, page: 'contact' },
          ].map(({ l, h, page }) => {
            const active = page && isActive(page);
            return (
              <a key={l} href={h} style={{
                display: 'block',
                color: active ? '#FFD12E' : lc,
                fontWeight: active ? 700 : 500,
                fontSize: 14, textDecoration: 'none',
                padding: '12px 0',
                borderBottom: `1px solid ${dark ? 'rgba(255,255,255,0.06)' : '#f0f2f8'}`,
              }}>{l}</a>
            );
          })}
        </div>
      )}
    </nav>
  );
}
