/* ============================================================
   UI SHELL — Nav, Footer, shared components
   ============================================================ */

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

/* Icons */
const Icon = {
  menu: (p) => <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" {...p}><path d="M3 6h18M3 12h18M3 18h18"/></svg>,
  x: (p) => <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" {...p}><path d="M18 6L6 18M6 6l12 12"/></svg>,
  arrow: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M5 12h14M13 5l7 7-7 7"/></svg>,
  play: (p) => <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor" {...p}><path d="M8 5v14l11-7z"/></svg>,
  search: (p) => <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" {...p}><circle cx="11" cy="11" r="8"/><path d="M21 21l-4.35-4.35"/></svg>,
  calendar: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="3" y="4" width="18" height="18" rx="2"/><path d="M16 2v4M8 2v4M3 10h18"/></svg>,
  mapPin: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0118 0z"/><circle cx="12" cy="10" r="3"/></svg>,
  clock: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>,
  facebook: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" {...p}><path d="M9 8h-3v4h3v12h5v-12h3.642l.358-4h-4v-1.667c0-.955.192-1.333 1.115-1.333h2.885v-5h-3.808c-3.596 0-5.192 1.583-5.192 4.615v3.385z"/></svg>,
  instagram: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="2" y="2" width="20" height="20" rx="5"/><path d="M16 11.37a4 4 0 11-7.914 1.173 4 4 0 017.914-1.173zM17.5 6.5h.01"/></svg>,
  youtube: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" {...p}><path d="M23.498 6.186a2.99 2.99 0 00-2.103-2.117C19.606 3.5 12 3.5 12 3.5s-7.606 0-9.395.569A2.99 2.99 0 00.502 6.186C0 7.999 0 12 0 12s0 4.001.502 5.814a2.99 2.99 0 002.103 2.117C4.394 20.5 12 20.5 12 20.5s7.606 0 9.395-.569a2.99 2.99 0 002.103-2.117C24 16.001 24 12 24 12s0-4.001-.502-5.814zM9.546 15.568V8.432L15.818 12l-6.272 3.568z"/></svg>,
  phone: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07 19.5 19.5 0 01-6-6 19.79 19.79 0 01-3.07-8.67A2 2 0 014.11 2h3a2 2 0 012 1.72c.127.96.361 1.903.7 2.81a2 2 0 01-.45 2.11L8.09 9.91a16 16 0 006 6l1.27-1.27a2 2 0 012.11-.45c.907.339 1.85.573 2.81.7A2 2 0 0122 16.92z"/></svg>,
  mail: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>,
  chevron: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><polyline points="9 18 15 12 9 6"/></svg>,
  heart: (p) => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M20.84 4.61a5.5 5.5 0 00-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 00-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 000-7.78z"/></svg>,
  pin: (p) => <svg width="40" height="40" viewBox="0 0 24 24" fill="currentColor" {...p}><path d="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5a2.5 2.5 0 010-5 2.5 2.5 0 010 5z"/></svg>,
};

const BrandLogo = ({ height = 36, variant = 'default', stacked = true }) => {
  // Logo: "BETHANY" in blue (Avenir Black), "BAPTIST CHURCH" beneath (Avenir Heavy)
  // "BAPTIST CHURCH" always spans the full width of "BETHANY"
  const bethanyColor = '#0099FF';
  const subColor = variant === 'light' ? '#FFFFFF' : '#666666';
  // sizing: BETHANY takes ~73% of height, BAPTIST CHURCH ~18% (visual balance matching the logo)
  const bethanySize = height * 0.72;
  const subSize = height * 0.26;
  return (
    <span
      style={{
        display: 'inline-flex',
        flexDirection: 'column',
        alignItems: 'stretch',
        lineHeight: 1,
        fontFamily: '"Avenir Next", Avenir, "Nunito Sans", "Helvetica Neue", Helvetica, Arial, sans-serif',
        userSelect: 'none',
      }}
      aria-label="Bethany Baptist Church"
    >
      <span style={{
        fontWeight: 800,
        fontSize: bethanySize,
        color: bethanyColor,
        letterSpacing: '-0.01em',
        lineHeight: 0.92,
      }}>BETHANY</span>
      <span style={{
        fontWeight: 800,
        fontSize: subSize,
        color: subColor,
        display: 'flex',
        justifyContent: 'space-between',
        width: '100%',
        marginTop: height * 0.05,
        letterSpacing: 0,
      }}>
        {'BAPTIST CHURCH'.split('').map((c, i) => (
          <span key={i} style={{ display: 'inline-block' }}>{c === ' ' ? '\u00A0' : c}</span>
        ))}
      </span>
    </span>
  );
};
// Kept for backwards compat in footer if needed
const BrandMark = ({ size = 40 }) => (
  <div className="brand-mark" style={{ width: size, height: size, fontSize: size*0.5 }}>
    <svg width={size*0.55} height={size*0.55} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M12 3v18M7 8v13M17 8v13M7 8a5 5 0 0110 0" strokeLinecap="round"/>
    </svg>
  </div>
);

function TopBar() {
  return (
    <div className="topbar">
      <div className="container">
        <div className="row" style={{ gap: 16, alignItems: 'center' }}>
          <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6 }}>
            <Icon.clock/> Sundays 11:00 AM & 6:30 PM
          </span>
          <span className="sep" style={{ display: 'none' }}>·</span>
          <span className="sep" style={{ display: 'inline' }}>·</span>
          <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6 }}>
            <Icon.mapPin/> 23–27 Gransha Road, Bangor, BT20 4TN
          </span>
        </div>
        <div className="row" style={{ gap: 14 }}>
          <a href="#/give">Give</a>
          <span className="sep">·</span>
          <a href="#/contact">Contact</a>
        </div>
      </div>
    </div>
  );
}

function Nav({ route, onNav }) {
  const [drawerOpen, setDrawerOpen] = useState(false);
  const [megaOpen, setMegaOpen] = useState(false);
  const navMode = document.documentElement.getAttribute('data-nav') || 'horizontal';

  return (
    <>
      <TopBar/>
      <header className="nav">
        <div className="container nav-inner">
          <a href="#/home" className="brand" onClick={() => setDrawerOpen(false)} style={{ gap: 0 }}>
            <BrandLogo height={44}/>
          </a>

          {navMode !== 'mega' ? (
            <nav className="nav-links">
              {NAV.filter(n => n.slug !== 'home' && n.slug !== 'give' && n.slug !== 'contact').map(n => (
                <a key={n.slug} href={`#/${n.slug}`}
                   className={route === n.slug ? 'active' : ''}>
                  {n.label}
                </a>
              ))}
            </nav>
          ) : (
            <nav className="nav-links" style={{ position: 'relative' }}>
              <a href="#/about" className={route==='about' ? 'active' : ''}>About</a>
              <a href="#/sermons" className={route==='sermons' ? 'active' : ''}>Sermons</a>
              <a href="#/events" className={route==='events' ? 'active' : ''}>Events</a>
              <a href="#/ministries"
                 className={route==='ministries' ? 'active' : ''}
                 onMouseEnter={() => setMegaOpen(true)}
                 onMouseLeave={() => setMegaOpen(false)}>
                Ministries <Icon.chevron style={{ marginLeft: 4 }}/>
              </a>
              <a href="#/visit" className={route==='visit' ? 'active' : ''}>Visit</a>
            </nav>
          )}

          <div className="row" style={{ gap: 8 }}>
            <a href="#/visit" className="btn btn-accent nav-cta">
              Plan a Visit <Icon.arrow/>
            </a>
            <button className="menu-btn" onClick={() => setDrawerOpen(true)} aria-label="Open menu">
              <Icon.menu/>
            </button>
          </div>
        </div>

        {navMode === 'mega' && (
          <div className={`mega ${megaOpen ? 'open' : ''}`}
               onMouseEnter={() => setMegaOpen(true)}
               onMouseLeave={() => setMegaOpen(false)}>
            <div className="container">
              <div className="mega-grid">
                <div className="mega-col">
                  <h4>For Families</h4>
                  <a href="#/ministries/children">Children <small>Sundays · Ages 0–11</small></a>
                  <a href="#/ministries/youth">Youth <small>Fridays · Ages 11–18</small></a>
                  <a href="#/ministries/parents-toddlers">Parents & Toddlers <small>Tuesdays · Ages 0–4</small></a>
                </div>
                <div className="mega-col">
                  <h4>Community</h4>
                  <a href="#/ministries/pickleball">Pickleball Club <small>Thursdays · All levels</small></a>
                  <a href="#/ministries">Small Groups <small>Weeknights across town</small></a>
                  <a href="#/ministries">Outreach <small>Food bank & mission</small></a>
                </div>
                <div className="mega-col">
                  <h4>Gather & Grow</h4>
                  <a href="#/ministries">Golden Fellowship <small>Monthly · 60+</small></a>
                  <a href="#/ministries">Worship Team <small>Musicians & tech</small></a>
                  <a href="#/ministries">All Ministries <small>See full list →</small></a>
                </div>
              </div>
            </div>
          </div>
        )}
      </header>

      {/* Mobile drawer */}
      <div className={`drawer ${drawerOpen ? 'open' : ''}`}>
        <div className="drawer-bg" onClick={() => setDrawerOpen(false)}/>
        <div className="drawer-panel">
          <div style={{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom: 20 }}>
            <BrandLogo height={40}/>
            <button className="menu-btn" onClick={() => setDrawerOpen(false)}><Icon.x/></button>
          </div>
          {NAV.map(n => (
            <a key={n.slug} href={`#/${n.slug}`}
               className={route === n.slug ? 'active' : ''}
               onClick={() => setDrawerOpen(false)}>
              {n.label}
            </a>
          ))}
          <a href="#/visit" className="btn btn-accent" style={{ marginTop: 20, width: '100%', justifyContent: 'center' }}>
            Plan a Visit <Icon.arrow/>
          </a>
        </div>
      </div>
    </>
  );
}

function Sidebar({ route }) {
  return (
    <aside className="sidebar">
      <div className="sb-label">Worship</div>
      <a href="#/home" className={route==='home' ? 'active' : ''}>Home</a>
      <a href="#/sermons" className={route==='sermons' ? 'active' : ''}>Sermons</a>
      <a href="#/events" className={route==='events' ? 'active' : ''}>Events</a>
      <div className="sb-label">Family</div>
      <a href="#/ministries" className={route==='ministries' ? 'active' : ''}>Ministries</a>
      <a href="#/ministries/children">Children</a>
      <a href="#/ministries/youth">Youth</a>
      <a href="#/ministries/parents-toddlers">Parents & Toddlers</a>
      <a href="#/ministries/pickleball">Pickleball Club</a>
      <div className="sb-label">More</div>
      <a href="#/about" className={route==='about' ? 'active' : ''}>About</a>
      <a href="#/visit" className={route==='visit' ? 'active' : ''}>Visit</a>
      <a href="#/give" className={route==='give' ? 'active' : ''}>Give</a>
      <a href="#/contact" className={route==='contact' ? 'active' : ''}>Contact</a>
    </aside>
  );
}

function Footer() {
  return (
    <footer>
      <div className="container">
        <div className="ft-grid">
          <div className="ft-about">
            <div className="ft-brand" style={{ display: 'block' }}>
              <BrandLogo height={64} variant="light"/>
            </div>
            <p style={{ marginTop: 20 }}>A family of believers in the heart of the community — gathering to worship Jesus, grow together, and serve the world around us since 1887.</p>
            <div className="ft-give">
              <div>
                <div style={{ fontSize: 11, letterSpacing: '0.14em', textTransform: 'uppercase', opacity: 0.7 }}>Support our ministry</div>
                <strong>Give today</strong>
              </div>
              <a href="#/give" className="btn">Donate</a>
            </div>
          </div>
          <div>
            <h4>Visit</h4>
            <a href="#/visit">Service Times</a>
            <a href="#/visit">Plan a Visit</a>
            <a href="#/visit">New Here?</a>
            <a href="#/contact">Get in Touch</a>
          </div>
          <div>
            <h4>Connect</h4>
            <a href="#/sermons">Sermons</a>
            <a href="#/events">Events</a>
            <a href="#/ministries">Ministries</a>
            <a href="#/give">Give</a>
          </div>
          <div>
            <h4>Contact</h4>
            <a href="https://www.google.com/maps/search/?api=1&query=23-27+Gransha+Road+Bangor+BT20+4TN" target="_blank" rel="noopener"><Icon.mapPin/> <span style={{marginLeft:6}}>23–27 Gransha Road, BT20 4TN</span></a>
            <a href="tel:02891459211"><Icon.phone/> <span style={{marginLeft:6}}>028 9145 9211</span></a>
            <a href="mailto:info@bethanybaptist.co.uk"><Icon.mail/> <span style={{marginLeft:6}}>info@bethanybaptist.co.uk</span></a>
          </div>
        </div>
        <div className="ft-bottom">
          <div>© 2026 Bethany Baptist Church, Bangor · NI Charities Commission No. 108952</div>
          <div className="ft-social">
            <a href="#" aria-label="Facebook"><Icon.facebook/></a>
            <a href="#" aria-label="Instagram"><Icon.instagram/></a>
            <a href="https://www.youtube.com/@bethanybaptistbangor" target="_blank" rel="noopener" aria-label="YouTube"><Icon.youtube/></a>
          </div>
        </div>
      </div>
    </footer>
  );
}

function ServicesBar() {
  return (
    <div className="services">
      <div className="container">
        <h3>Join us this Sunday</h3>
        <div className="service-times">
          {SERVICE_TIMES.map((s,i) => (
            <div key={i} className="service-time">
              <div className="t-label">{s.label}</div>
              <div className="t-time">{s.time}</div>
              <div style={{ fontSize: 12, opacity: 0.75, marginTop: 2 }}>{s.note}</div>
            </div>
          ))}
        </div>
        <a href="#/visit" className="btn">Plan a Visit <Icon.arrow/></a>
      </div>
    </div>
  );
}

function VerseBlock() {
  return (
    <section style={{ background: 'var(--c-bg-2)', borderTop: '1px solid var(--c-line)', borderBottom: '1px solid var(--c-line)' }}>
      <div className="container">
        <div className="verse-block">
          <blockquote>
            For we are his workmanship, created in Christ Jesus for good works, which God prepared beforehand, that we should walk in them.
          </blockquote>
          <cite>— Ephesians 2:10</cite>
        </div>
      </div>
    </section>
  );
}

function PageHead({ crumbs, title, lede, chips }) {
  return (
    <div className="page-head">
      <div className="container">
        {crumbs && <div className="breadcrumbs">{crumbs.map((c,i) => (
          <React.Fragment key={i}>
            {i > 0 && <span style={{ opacity: 0.4, margin: '0 8px' }}>/</span>}
            {c.href ? <a href={c.href}>{c.label}</a> : c.label}
          </React.Fragment>
        ))}</div>}
        <h1>{title}</h1>
        {lede && <p className="lede">{lede}</p>}
        {chips && <div className="chip-row">{chips.map((c,i) => (
          <span key={i} className="chip">{c}</span>
        ))}</div>}
      </div>
    </div>
  );
}

Object.assign(window, { Icon, BrandMark, BrandLogo, Nav, Sidebar, Footer, ServicesBar, VerseBlock, PageHead });
