// Top nav + footer + small shared bits.

const Nav = ({ lang, setLang, theme, setTheme }) => {
  const [scrolled, setScrolled] = React.useState(false);
  const [open, setOpen] = React.useState(false);

  React.useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 8);
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  // Detect if we're on the homepage so we can use in-page smooth-scroll
  // anchors there, and full URLs everywhere else.
  const isHome = typeof window !== 'undefined' &&
    /(^|\/)index\.html?$|\/$/.test(window.location.pathname);

  // Each link has an explicit href to a dedicated page where one exists.
  // Aanpak and Diensten are now full V2 pages (aanpak.html / diensten.html).
  // FAQ stays as a homepage anchor since it lives on the homepage.
  const links = [
    { id: 'aanpak',   key: 'nav.approach', href: 'aanpak.html' },
    { id: 'cases',    key: 'nav.cases',    href: 'cases.html' },
    { id: 'diensten', key: 'nav.services', href: 'diensten.html' },
    { id: 'prijzen',  key: 'nav.pricing',  href: 'prijzen.html' },
    { id: 'faq',      key: 'nav.faq',      href: isHome ? '#faq'      : 'index.html#faq' },
    { id: 'over',     key: 'nav.about',    href: 'team.html' },
    { id: 'contact',  key: 'nav.contact',  href: 'contact.html' },
  ];

  return (
    <header
      style={{
        position: 'sticky',
        top: 0,
        zIndex: 50,
        background: scrolled ? 'color-mix(in oklab, var(--bg-base) 80%, transparent)' : 'transparent',
        backdropFilter: scrolled ? 'saturate(180%) blur(12px)' : 'none',
        WebkitBackdropFilter: scrolled ? 'saturate(180%) blur(12px)' : 'none',
        borderBottom: scrolled ? '1px solid var(--border)' : '1px solid transparent',
        transition: 'background-color 200ms var(--ease), border-color 200ms var(--ease)',
      }}
    >
      {/* 3-column grid puts the nav links in a centred column while logo
          stays left and lang/theme/CTA stay right. Equal-width side
          columns keep the menu visually centred even if the right-side
          stack grows. */}
      <div className="container modus-nav__grid" style={{
        display: 'grid',
        gridTemplateColumns: '1fr auto 1fr',
        alignItems: 'center',
        height: 64,
        gap: 16,
      }}>
        <a href={isHome ? '#top' : 'index.html'}
           style={{ display: 'inline-flex', alignItems: 'center', justifySelf: 'start' }}
           aria-label="MODUS: Home">
          <Logo variant={window.__logoVariant || 'mono'} size={26} />
        </a>

        <nav className="hide-mobile" style={{ display: 'flex', gap: 4, justifySelf: 'center' }}>
          {links.map(l => (
            <a
              key={l.id}
              href={l.href}
              className="btn-ghost"
              style={{
                padding: '8px 12px',
                fontSize: '0.9375rem',
                color: 'var(--ink-secondary)',
                fontWeight: 500,
                borderRadius: 6,
                transition: 'color 180ms var(--ease)',
              }}
              onMouseEnter={(e) => e.currentTarget.style.color = 'var(--accent)'}
              onMouseLeave={(e) => e.currentTarget.style.color = 'var(--ink-secondary)'}
            >
              {tr(lang, l.key)}
            </a>
          ))}
        </nav>

        <div style={{ display: 'flex', alignItems: 'center', gap: 12, justifySelf: 'end' }}>
          {/* Order: CTA -> LangToggle -> Burger. LangToggle sits between
              CTA and burger so it never overlaps the logo on narrow phones
              (previous Lang-first order made the lang chip wrap onto the
              logo at <380px when col 3 was tight). */}
          <a href="https://cal.eu/modusops/quick-scan"
             data-cal-link="modusops/quick-scan"
             data-cal-config='{"layout":"month_view","theme":"dark"}'
             onClick={(e) => {
               if (window.Cal) {
                 e.preventDefault();
                 window.Cal('modal', { calLink: 'modusops/quick-scan', config: { layout: 'month_view', theme: 'dark', language: lang } });
               }
             }}
             className="btn modus-nav__cta">
            {/* Show "Quick" only on desktop (CSS in styles.css hides
                .nav-cta-q at <=767px). Keeps the button compact on mobile
                next to the lang chip + burger.
                Use explicit {' '} markers to guarantee Babel-standalone
                does not collapse whitespace between text nodes and spans. */}
            {lang === 'nl'
              ? <>Boek{' '}<span className="nav-cta-q">Quick{' '}</span>Scan</>
              : <>Book{' '}<span className="nav-cta-q">Quick{' '}</span>Scan</>}
          </a>
          <LangToggle lang={lang} setLang={setLang} />
          <button
            className="btn-ghost nav-burger"
            aria-label="Menu"
            aria-expanded={open}
            onClick={() => setOpen(!open)}
            style={{
              padding: 8,
              border: '1px solid var(--border)',
              borderRadius: 6,
              background: 'transparent',
              color: 'var(--ink-primary)',
              cursor: 'pointer',
            }}
          >
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8">
              {open
                ? <><path d="M18 6 6 18"/><path d="M6 6l12 12"/></>
                : <><path d="M4 7h16"/><path d="M4 12h16"/><path d="M4 17h16"/></>
              }
            </svg>
          </button>
        </div>
      </div>

      {open && (
        <div style={{
          borderTop: '1px solid var(--border)',
          background: 'var(--bg-base)',
          padding: '12px var(--gutter) 20px',
        }}>
          {links.map(l => (
            <a key={l.id} href={l.href} onClick={() => setOpen(false)}
              style={{ display: 'block', padding: '10px 0', color: 'var(--ink-primary)', fontSize: 16 }}>
              {tr(lang, l.key)}
            </a>
          ))}
          {/* LangToggle no longer here - it's always visible in the nav bar */}
          <a href="https://cal.eu/modusops/quick-scan"
             data-cal-link="modusops/quick-scan"
             data-cal-config='{"layout":"month_view","theme":"dark"}'
             onClick={(e) => {
               setOpen(false);
               if (window.Cal) {
                 e.preventDefault();
                 window.Cal('modal', { calLink: 'modusops/quick-scan', config: { layout: 'month_view', theme: 'dark', language: lang } });
               }
             }}
             className="btn btn-primary btn-block"
             style={{ marginTop: 12, textTransform: 'uppercase', fontWeight: 700, letterSpacing: '0.06em' }}>
            {tr(lang, 'nav.cta')}
          </a>
        </div>
      )}
    </header>
  );
};

// Uses shared .modus-lang CSS in styles.css so React + static pages
// render the same visual. Click handler swaps the React app's lang
// state (no navigation needed).
const LangToggle = ({ lang, setLang }) => (
  <div className="modus-lang" role="group" aria-label="Language">
    {['nl', 'en'].map(L => (
      <button
        key={L}
        type="button"
        onClick={() => setLang(L)}
        aria-pressed={lang === L}
      >
        {L}
      </button>
    ))}
  </div>
);

const ThemeToggle = ({ theme, setTheme }) => {
  const next = theme === 'dark' ? 'light' : 'dark';
  return (
    <button
      onClick={() => setTheme(next)}
      aria-label={`Switch to ${next} mode`}
      style={{
        display: 'inline-flex',
        alignItems: 'center',
        justifyContent: 'center',
        width: 32, height: 32,
        border: '1px solid var(--border)',
        borderRadius: 6,
        background: 'var(--bg-raised)',
        color: 'var(--ink-primary)',
        cursor: 'pointer',
        transition: 'border-color 180ms var(--ease)',
      }}
    >
      {theme === 'dark' ? (
        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
          <circle cx="12" cy="12" r="4"/>
          <path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41"/>
        </svg>
      ) : (
        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
          <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
        </svg>
      )}
    </button>
  );
};

const Footer = ({ lang, setLang }) => (
  <footer style={{
    borderTop: '1px solid var(--border)',
    padding: '64px 0 40px',
    background: 'var(--bg-base)',
  }}>
    <div className="container">
      <div style={{
        display: 'grid',
        gridTemplateColumns: 'repeat(auto-fit, minmax(180px, 1fr))',
        gap: 32,
        marginBottom: 56,
      }}>
        <div>
          <Logo variant={window.__logoVariant || 'mono'} size={26} />
          <p className="secondary" style={{ marginTop: 16, fontSize: 14, maxWidth: 280 }}>
            {tr(lang, 'footer.tag')}
          </p>
        </div>
        <FooterCol title={tr(lang, 'footer.col.product')} links={[
          { label: lang === 'en' ? 'Quick Scan - €1,199' : 'Quick Scan - €1.199', href: lang === 'en' ? 'quick-scan-en.html' : 'quick-scan.html' },
          { label: lang === 'en' ? 'Full Scan - from €2,999' : 'Full Scan - vanaf €2.999', href: 'prijzen.html' },
          { label: lang === 'en' ? 'Build Sprint' : 'Build Sprint',                       href: 'prijzen.html' },
          { label: lang === 'en' ? 'Care - from €495/mo' : 'Care - vanaf €495/mo',        href: 'prijzen.html#care' },
          { label: lang === 'en' ? 'Web & findability' : 'Web & vindbaarheid',            href: 'prijzen.html' },
          { label: tr(lang, 'nav.pricing'),  href: 'prijzen.html' },
        ]} />
        <FooterCol title={tr(lang, 'footer.col.company')} links={[
          { label: tr(lang, 'nav.approach'), href: 'aanpak.html' },
          { label: tr(lang, 'nav.services'), href: 'diensten.html' },
          { label: tr(lang, 'nav.cases'),    href: 'cases.html' },
          { label: tr(lang, 'nav.faq'),      href: 'index.html#faq' },
          { label: tr(lang, 'nav.about'),    href: 'team.html' },
          { label: tr(lang, 'nav.contact'),  href: 'contact.html' },
        ]} />
        <FooterCol title={tr(lang, 'footer.col.legal')} links={[
          { label: tr(lang, 'footer.legal.privacy'), href: 'privacy.html' },
          { label: tr(lang, 'footer.legal.terms'), href: 'voorwaarden.html' },
        ]} />
      </div>
      <div style={{
        display: 'flex',
        flexWrap: 'wrap',
        justifyContent: 'space-between',
        alignItems: 'center',
        gap: 16,
        paddingTop: 24,
        borderTop: '1px solid var(--border)',
      }}>
        <span className="mono" style={{ color: 'var(--ink-muted)' }}>
          {tr(lang, 'footer.copy')}
        </span>
        <div style={{ display: 'inline-flex', alignItems: 'center', gap: 12 }}>
          {setLang && <LangToggle lang={lang} setLang={setLang} />}
          <span className="mono" style={{ color: 'var(--ink-muted)' }}>{lang === 'en' ? 'ALL PRICES EX VAT' : 'ALLE PRIJZEN EX BTW'}</span>
        </div>
      </div>
    </div>
  </footer>
);

const FooterCol = ({ title, links }) => (
  <div>
    <div className="mono" style={{ color: 'var(--ink-muted)', marginBottom: 14 }}>{title}</div>
    <ul style={{ listStyle: 'none', padding: 0, margin: 0, display: 'flex', flexDirection: 'column', gap: 10 }}>
      {links.map(l => (
        <li key={l.label}>
          <a href={l.href} style={{ color: 'var(--ink-secondary)', fontSize: 14 }}
            onMouseEnter={e => e.currentTarget.style.color = 'var(--accent)'}
            onMouseLeave={e => e.currentTarget.style.color = 'var(--ink-secondary)'}>
            {l.label}
          </a>
        </li>
      ))}
    </ul>
  </div>
);

window.Nav = Nav;
window.Footer = Footer;
