// shared.jsx — APABA shared components & primitives

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

// ─────────────────────────────────────────────────────────────
// Photo placeholder — documentary-style with monospace shot notes
function Photo({
  ratio = "16/9",
  tone = "warm",
  label,
  shot,
  height,
  children,
  noNote = false,
  style = {},
}) {
  const aspectStyle = height ? { height } : { aspectRatio: ratio.replace('/', ' / ') };
  return (
    <div
      className={`photo-placeholder photo-tone-${tone}`}
      style={{ ...aspectStyle, width: '100%', ...style }}
    >
      {!noNote && (label || shot) && (
        <div className="photo-note">
          {label && <span className="photo-note-label">{label}</span>}
          {shot}
        </div>
      )}
      {children}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Top utility bar — entity toggle, login, donate
function UtilityBar({ entity, setEntity, currentPage, navigate }) {
  return (
    <div style={{
      background: 'var(--ink)',
      color: 'var(--paper)',
      fontSize: 12.5,
      fontFamily: 'var(--mono)',
      letterSpacing: '0.04em',
      borderBottom: '1px solid rgba(255,255,255,0.08)',
      position: 'relative',
      zIndex: 50,
    }}>
      <div className="container-wide" style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', height: 38 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 0 }}>
          <button
            onClick={() => setEntity('apaba')}
            style={{
              padding: '8px 14px',
              color: entity === 'apaba' ? 'var(--paper)' : 'rgba(250,246,240,0.55)',
              borderBottom: entity === 'apaba' ? '2px solid var(--accent)' : '2px solid transparent',
              fontFamily: 'var(--mono)',
              fontSize: 11.5,
              letterSpacing: '0.1em',
              textTransform: 'uppercase',
              transition: 'color 160ms',
            }}
          >APABA Colorado</button>
          <button
            onClick={() => { setEntity('foundation'); navigate('foundation'); }}
            style={{
              padding: '8px 14px',
              color: entity === 'foundation' ? 'var(--paper)' : 'rgba(250,246,240,0.55)',
              borderBottom: entity === 'foundation' ? '2px solid var(--gold)' : '2px solid transparent',
              fontFamily: 'var(--mono)',
              fontSize: 11.5,
              letterSpacing: '0.1em',
              textTransform: 'uppercase',
            }}
          >APABA Foundation</button>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 24, fontSize: 11.5, letterSpacing: '0.08em', textTransform: 'uppercase' }}>
          <a href="#" style={{ opacity: 0.75 }} onClick={(e) => { e.preventDefault(); navigate('clinic'); }}>Free Legal Clinic</a>
          <a href="#" style={{ opacity: 0.75 }}>Member Login</a>
          <a href="#" onClick={(e) => { e.preventDefault(); navigate('foundation'); }} style={{ color: 'var(--gold)' }}>Donate</a>
        </div>
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Main header / nav
function Header({ navigate, currentPage }) {
  const [scrolled, setScrolled] = useState(false);
  const [mobileOpen, setMobileOpen] = useState(false);

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

  const links = [
    { id: 'about', label: 'About' },
    { id: 'membership', label: 'Membership' },
    { id: 'events', label: 'Events' },
    { id: 'clinic', label: 'Free Legal Clinic' },
    { id: 'statements', label: 'Public Statements' },
  ];

  return (
    <header style={{
      position: 'sticky',
      top: 0,
      zIndex: 40,
      background: 'rgba(250, 246, 240, 0.92)',
      backdropFilter: 'blur(12px)',
      WebkitBackdropFilter: 'blur(12px)',
      borderBottom: scrolled ? '1px solid var(--rule)' : '1px solid transparent',
      transition: 'border-color 200ms',
    }}>
      <div className="container-wide" style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', height: 76 }}>
        <a href="#" onClick={(e) => { e.preventDefault(); navigate('home'); }} style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
          <Wordmark />
        </a>

        <nav style={{ display: 'flex', alignItems: 'center', gap: 36 }} className="apaba-nav">
          {links.map(l => (
            <a
              key={l.id}
              href="#"
              onClick={(e) => { e.preventDefault(); navigate(l.id); }}
              style={{
                fontFamily: 'var(--sans)',
                fontSize: 14,
                fontWeight: 500,
                letterSpacing: '0.005em',
                color: currentPage === l.id ? 'var(--accent)' : 'var(--charcoal)',
                position: 'relative',
                paddingBottom: 4,
                borderBottom: currentPage === l.id ? '1px solid var(--accent)' : '1px solid transparent',
                transition: 'all 160ms',
              }}
            >{l.label}</a>
          ))}
        </nav>

        <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
          <button className="btn btn-ghost" style={{ padding: '10px 16px', fontSize: 13 }}>Sign in</button>
          <button onClick={() => navigate('membership')} className="btn btn-primary" style={{ padding: '12px 20px', fontSize: 13 }}>Join APABA</button>
        </div>
      </div>
    </header>
  );
}

// ─────────────────────────────────────────────────────────────
// Wordmark — bespoke type lockup, no logo file needed
function Wordmark() {
  return (
    <div style={{ display: 'flex', alignItems: 'baseline', gap: 10 }}>
      <span style={{
        fontFamily: 'var(--serif)',
        fontSize: 28,
        fontWeight: 500,
        letterSpacing: '-0.01em',
        color: 'var(--charcoal)',
        lineHeight: 1,
      }}>APABA</span>
      <span style={{
        fontFamily: 'var(--serif)',
        fontStyle: 'italic',
        fontSize: 18,
        fontWeight: 400,
        color: 'var(--accent)',
        lineHeight: 1,
      }}>Colorado</span>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Footer
function Footer({ navigate }) {
  return (
    <footer style={{
      background: 'var(--ink-deeper)',
      color: 'var(--paper)',
      paddingTop: 96,
      paddingBottom: 48,
      marginTop: 'var(--rhythm)',
    }}>
      <div className="container-wide">
        <div style={{ display: 'grid', gridTemplateColumns: '1.4fr 1fr 1fr 1fr', gap: 60, paddingBottom: 80 }}>
          <div>
            <div style={{ display: 'flex', alignItems: 'baseline', gap: 10, marginBottom: 24 }}>
              <span style={{ fontFamily: 'var(--serif)', fontSize: 32, fontWeight: 500, letterSpacing: '-0.01em' }}>APABA</span>
              <span style={{ fontFamily: 'var(--serif)', fontStyle: 'italic', fontSize: 22, color: 'var(--accent)' }}>Colorado</span>
            </div>
            <p style={{ fontSize: 15, lineHeight: 1.6, color: 'rgba(250,246,240,0.72)', maxWidth: 360, fontFamily: 'var(--serif)' }}>
              The Asian Pacific American Bar Association of Colorado has served attorneys, judges, and law students across the state for thirty-five years.
            </p>
            <div style={{ marginTop: 28, fontFamily: 'var(--mono)', fontSize: 12, lineHeight: 1.7, color: 'rgba(250,246,240,0.55)' }}>
              <div>P.O. Box 18585</div>
              <div>Denver, Colorado 80218</div>
              <div style={{ marginTop: 8 }}>info@apabacolorado.org</div>
            </div>
          </div>
          <FooterCol title="Association" links={[
            ['Mission', 'about'], ['Board of Directors', 'about'], ['Committees', 'about'],
            ['Past Presidents', 'about'], ['Bylaws', 'about'], ['Contact', 'about'],
          ]} navigate={navigate} />
          <FooterCol title="Programs" links={[
            ['Membership', 'membership'], ['Free Legal Clinic', 'clinic'], ['Annual Banquet', 'events'],
            ['Mentorship', 'about'], ['Public Statements', 'statements'],
          ]} navigate={navigate} />
          <FooterCol title="Foundation" links={[
            ['About the Foundation', 'foundation'], ['Scholarships', 'foundation'], ['Grants', 'foundation'],
            ['Colorado Gives Day', 'foundation'], ['Donate', 'foundation'],
          ]} navigate={navigate} />
        </div>
        <div style={{
          paddingTop: 28,
          borderTop: '1px solid rgba(250,246,240,0.12)',
          display: 'flex',
          justifyContent: 'space-between',
          alignItems: 'center',
          fontFamily: 'var(--mono)',
          fontSize: 11.5,
          letterSpacing: '0.05em',
          color: 'rgba(250,246,240,0.45)',
        }}>
          <div>© 2026 APABA Colorado · NAPABA Affiliate</div>
          <div style={{ display: 'flex', gap: 24 }}>
            <a href="#" style={{ opacity: 0.85 }}>LinkedIn</a>
            <a href="#" style={{ opacity: 0.85 }}>Instagram</a>
            <a href="#" style={{ opacity: 0.85 }}>Privacy</a>
          </div>
        </div>
      </div>
    </footer>
  );
}

function FooterCol({ title, links, navigate }) {
  return (
    <div>
      <div style={{
        fontFamily: 'var(--mono)',
        fontSize: 11,
        letterSpacing: '0.18em',
        textTransform: 'uppercase',
        color: 'rgba(250,246,240,0.5)',
        marginBottom: 18,
      }}>{title}</div>
      <ul style={{ listStyle: 'none', padding: 0, margin: 0, display: 'flex', flexDirection: 'column', gap: 12 }}>
        {links.map(([label, page]) => (
          <li key={label}>
            <a
              href="#"
              onClick={(e) => { e.preventDefault(); navigate(page); }}
              style={{ fontSize: 14, color: 'rgba(250,246,240,0.85)', transition: 'color 160ms' }}
              onMouseEnter={(e) => e.currentTarget.style.color = 'var(--accent)'}
              onMouseLeave={(e) => e.currentTarget.style.color = 'rgba(250,246,240,0.85)'}
            >{label}</a>
          </li>
        ))}
      </ul>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Generic page header (interior pages)
function PageHero({ eyebrow, title, italicWord, kicker }) {
  return (
    <section style={{ paddingTop: 72, paddingBottom: 56, borderBottom: '1px solid var(--rule)' }}>
      <div className="container-wide">
        <div className="eyebrow" style={{ marginBottom: 28 }}>{eyebrow}</div>
        <h1 className="display" style={{ maxWidth: 1100, marginBottom: 24 }}>
          {title}
          {italicWord && <> <em style={{ fontStyle: 'italic', color: 'var(--accent)' }}>{italicWord}</em></>}
        </h1>
        {kicker && (
          <p style={{ fontFamily: 'var(--serif)', fontSize: 22, lineHeight: 1.45, maxWidth: 720, color: 'var(--text-muted)' }}>
            {kicker}
          </p>
        )}
      </div>
    </section>
  );
}

// ─────────────────────────────────────────────────────────────
// Section break ornament — printer's flourish
function Ornament({ color = 'var(--rule)' }) {
  return (
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 18, color }}>
      <span style={{ width: 60, height: 1, background: 'currentColor' }} />
      <span style={{ fontFamily: 'var(--serif)', fontStyle: 'italic', fontSize: 18 }}>§</span>
      <span style={{ width: 60, height: 1, background: 'currentColor' }} />
    </div>
  );
}

// Export to window so other babel scripts can use them
Object.assign(window, {
  Photo, UtilityBar, Header, Footer, FooterCol, PageHero, Ornament, Wordmark,
});
