/* @jsx React.createElement */
const { useState } = React;

// ============ Masthead ============
function Masthead({ issue, dateline, onSubscribe }) {
  return (
    <header style={{ background: 'var(--af-paper)', borderBottom: '4px solid var(--af-ink)', padding: '32px 0 18px' }}>
      <div style={{ maxWidth: 680, margin: '0 auto', padding: '0 24px' }}>
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 14 }}>
          <span className="af-eyebrow">Local Mat Talk</span>
          <span className="af-eyebrow">{dateline}</span>
        </div>
        <img src="../../assets/logo/arlington-flip-wordmark.svg" alt="Arlington Flip"
             style={{ display: 'block', height: 110, width: 'auto', margin: '0 auto' }} />
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: 18 }}>
          <span style={{ display: 'inline-block', background: 'var(--af-flag)', color: 'var(--af-paper)', padding: '4px 10px 5px', fontSize: 12, letterSpacing: '0.14em', textTransform: 'uppercase', fontWeight: 700, fontFamily: 'var(--af-font-body)' }}>
            Issue {issue}
          </span>
          <a href="#subscribe" onClick={onSubscribe} style={{ fontSize: 13, fontWeight: 700, letterSpacing: '0.04em', textTransform: 'uppercase', whiteSpace: 'nowrap' }}>
            Subscribe Free →
          </a>
        </div>
      </div>
    </header>
  );
}

// ============ Issue header (above the fold) ============
function IssueHeader({ headline, dek }) {
  return (
    <section style={{ maxWidth: 680, margin: '0 auto', padding: '40px 24px 24px' }}>
      <h1 style={{ fontFamily: 'var(--af-font-display)', fontSize: 56, lineHeight: 0.95, letterSpacing: '-0.01em', textTransform: 'uppercase', margin: '0 0 16px' }}>
        {headline}
      </h1>
      <p style={{ fontSize: 19, lineHeight: 1.5, color: 'var(--af-ink-70)', margin: 0, fontFamily: 'var(--af-font-serif)', fontStyle: 'italic' }}>
        {dek}
      </p>
    </section>
  );
}

// ============ Section opener ============
function SectionOpener({ label, title, featured }) {
  return (
    <div style={{ maxWidth: 680, margin: '0 auto', padding: '32px 24px 4px' }}>
      <span className={featured ? 'af-eyebrow af-eyebrow--flag' : 'af-eyebrow af-eyebrow--flip'}>{label}</span>
      <h2 style={{ fontFamily: 'var(--af-font-display)', fontSize: 36, lineHeight: 1.0, textTransform: 'uppercase', letterSpacing: '-0.01em', margin: '6px 0 10px' }}>
        {title}
      </h2>
      <hr className="af-rule af-rule--bold" style={{ margin: '0 0 18px', borderColor: featured ? 'var(--af-flag)' : 'var(--af-ink)' }} />
    </div>
  );
}

// ============ This-month bullets ============
function ThisMonthList({ items }) {
  return (
    <ul style={{ maxWidth: 680, margin: '0 auto', padding: '0 24px', listStyle: 'none' }}>
      {items.map((it, i) => (
        <li key={i} style={{ display: 'flex', gap: 14, padding: '14px 0', borderBottom: '1px solid var(--af-ink-15)' }}>
          <span style={{ fontFamily: 'var(--af-font-display)', fontSize: 20, color: i === 0 ? 'var(--af-flag)' : 'var(--af-flip-deep)', minWidth: 28, lineHeight: 1.2 }}>
            {String(i + 1).padStart(2, '0')}
          </span>
          <span style={{ fontSize: 17, lineHeight: 1.5 }}>{it}</span>
        </li>
      ))}
    </ul>
  );
}

// ============ Story ============
function Story({ kicker, headline, body, hasQuote, quote, featured }) {
  return (
    <article style={{ maxWidth: 680, margin: '0 auto', padding: '24px 24px' }}>
      <span className="af-eyebrow" style={featured ? { color: 'var(--af-flag)' } : null}>{kicker}</span>
      <h3 style={{ fontFamily: 'var(--af-font-display)', fontSize: 28, lineHeight: 1.05, textTransform: 'uppercase', letterSpacing: '-0.01em', margin: '6px 0 14px' }}>
        {headline}
      </h3>
      {body.map((p, i) => (
        <p key={i} style={{ fontSize: 18, lineHeight: 1.6, margin: '0 0 14px' }}>{p}</p>
      ))}
      {hasQuote && (
        <blockquote style={{
          margin: '20px 0 8px',
          padding: '4px 0 4px 20px',
          borderLeft: '3px solid var(--af-flag)',
          fontFamily: 'var(--af-font-serif)',
          fontStyle: 'italic',
          fontSize: 22,
          lineHeight: 1.4,
          color: 'var(--af-ink-90)'
        }}>
          {quote}
        </blockquote>
      )}
    </article>
  );
}

// ============ Rotation rundown (small data table feel) ============
function RotationRundown({ rows }) {
  return (
    <div style={{ maxWidth: 680, margin: '0 auto', padding: '8px 24px 24px' }}>
      {rows.map((r, i) => (
        <div key={i} style={{ display: 'grid', gridTemplateColumns: '52px 1fr 90px', gap: 16, padding: '14px 0', borderBottom: '1px solid var(--af-ink-15)', alignItems: 'baseline' }}>
          <span style={{
            fontFamily: 'var(--af-font-display)',
            fontSize: 16,
            lineHeight: 1,
            textTransform: 'uppercase',
            display: 'inline-block',
            padding: '6px 8px 7px',
            background: r.place === '1st' ? 'var(--af-flag)' : 'transparent',
            color: r.place === '1st' ? 'var(--af-paper)' : 'var(--af-ink)',
            justifySelf: 'start',
            letterSpacing: '0.02em'
          }}>
            {r.place}
          </span>
          <div>
            <div style={{ fontSize: 17, fontWeight: 600 }}>{r.name}</div>
            <div style={{ fontSize: 13, color: 'var(--af-ink-50)', marginTop: 2 }}>{r.gym} · {r.event}</div>
          </div>
          <span style={{ fontFamily: 'var(--af-font-display)', fontSize: 18, textAlign: 'right', color: 'var(--af-flip-deep)' }}>
            {r.score}
          </span>
        </div>
      ))}
    </div>
  );
}

// ============ On-deck calendar ============
function OnDeck({ events }) {
  return (
    <div style={{ maxWidth: 680, margin: '0 auto', padding: '8px 24px 24px' }}>
      {events.map((e, i) => (
        <div key={i} style={{ display: 'flex', gap: 18, padding: '14px 0', borderBottom: '1px solid var(--af-ink-15)' }}>
          <div style={{ minWidth: 64, textAlign: 'center', borderRight: '2px solid var(--af-ink)', paddingRight: 14 }}>
            <div style={{ fontSize: 11, letterSpacing: '0.14em', textTransform: 'uppercase', color: 'var(--af-ink-50)' }}>{e.month}</div>
            <div style={{ fontFamily: 'var(--af-font-display)', fontSize: 32, lineHeight: 1 }}>{e.day}</div>
          </div>
          <div>
            <div style={{ fontSize: 17, fontWeight: 600, marginBottom: 2 }}>{e.title}</div>
            <div style={{ fontSize: 14, color: 'var(--af-ink-70)' }}>{e.where} · {e.time}</div>
          </div>
        </div>
      ))}
    </div>
  );
}

// ============ Subscribe block ============
function SubscribeBlock() {
  const [email, setEmail] = useState('');
  const [done, setDone] = useState(false);
  return (
    <section id="subscribe" style={{ background: 'var(--af-ink)', color: 'var(--af-paper)', padding: '48px 0', marginTop: 32 }}>
      <div style={{ maxWidth: 680, margin: '0 auto', padding: '0 24px' }}>
        <span className="af-eyebrow" style={{ color: 'var(--af-flip)' }}>The TLDR</span>
        <h2 style={{ fontFamily: 'var(--af-font-display)', fontSize: 40, lineHeight: 1, textTransform: 'uppercase', margin: '8px 0 14px', letterSpacing: '-0.01em' }}>
          Get this in your inbox.
        </h2>
        <p style={{ fontSize: 17, color: 'var(--af-ink-30)', margin: '0 0 24px', lineHeight: 1.5 }}>
          One email a month. The rotation rundown, the mat talk, the calendar. No spam, no fluff.
        </p>
        {done ? (
          <div style={{ fontSize: 16, color: 'var(--af-flag)' }}>✓ You're on the list. See you next month.</div>
        ) : (
          <form onSubmit={(e) => { e.preventDefault(); if (email) setDone(true); }}
                style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}>
            <input
              type="email" required value={email} onChange={(e) => setEmail(e.target.value)}
              placeholder="you@inbox.com"
              style={{ flex: 1, minWidth: 220, padding: '12px 14px', fontSize: 16, fontFamily: 'var(--af-font-body)',
                       background: 'var(--af-paper)', color: 'var(--af-ink)', border: '1px solid var(--af-paper)',
                       borderRadius: 6, outline: 'none' }} />
            <button type="submit" style={{ padding: '12px 22px', fontFamily: 'var(--af-font-body)', fontWeight: 700,
                       fontSize: 14, letterSpacing: '0.04em', textTransform: 'uppercase',
                       background: 'var(--af-flip)', color: 'var(--af-ink)', border: 'none', borderRadius: 6, cursor: 'pointer' }}>
              Subscribe Free
            </button>
          </form>
        )}
      </div>
    </section>
  );
}

// ============ Footer ============
function Footer() {
  return (
    <footer style={{ background: 'var(--af-paper-warm)', padding: '32px 0', borderTop: '1px solid var(--af-ink-15)' }}>
      <div style={{ maxWidth: 680, margin: '0 auto', padding: '0 24px', display: 'flex', justifyContent: 'space-between', flexWrap: 'wrap', gap: 12 }}>
        <div>
          <div style={{ fontFamily: 'var(--af-font-display)', textTransform: 'uppercase', fontSize: 18, letterSpacing: '-0.01em' }}>Arlington Flip</div>
          <div style={{ fontSize: 13, color: 'var(--af-ink-70)', marginTop: 4 }}>Local mat talk · Arlington, VA</div>
        </div>
        <div style={{ fontSize: 13, color: 'var(--af-ink-70)' }}>
          <a href="#" style={{ marginRight: 14 }}>Archive</a>
          <a href="#" style={{ marginRight: 14 }}>About</a>
          <a href="#">Tip us</a>
        </div>
      </div>
    </footer>
  );
}

Object.assign(window, {
  Masthead, IssueHeader, SectionOpener, ThisMonthList,
  Story, RotationRundown, OnDeck, SubscribeBlock, Footer,
});
