/* member.jsx — authenticated experience */
function AppShell({ user, route, onNav, children }) {
  const [menu, setMenu] = useState(false);
  useEffect(() => {
    if (!menu) return;
    const h = () => setMenu(false);
    const k = e => e.key === 'Escape' && setMenu(false);
    window.addEventListener('click', h); window.addEventListener('keydown', k);
    return () => { window.removeEventListener('click', h); window.removeEventListener('keydown', k); };
  }, [menu]);
  const tabs = [['table', 'The Table', 'users'], ['word', 'Daily Word', 'book-open'], ['library', 'Library', 'library']];
  const cur = route === 'journal' ? 'word' : route;
  return (
    <div className="app-body">
      <header className="app-top">
        <div className="wrap">
          <Brand onClick={() => onNav('table')} size={38} />
          <nav className="app-tabs" aria-label="Primary">
            {tabs.map(([id, l, ic]) => <button key={id} className="app-tab" aria-current={cur === id ? 'page' : undefined} onClick={() => onNav(id)}><Icon name={ic} />{l}</button>)}
          </nav>
          <div className="app-right" onClick={e => e.stopPropagation()}>
            <button className="avatar" aria-haspopup="menu" aria-expanded={menu} aria-label="Account menu" onClick={() => setMenu(!menu)}>{(user.name || 'Y')[0]}</button>
            {menu && (
              <div className="menu" role="menu">
                <div className="menu-head"><b>{user.name}</b><span>{(STAGES[user.stage] || STAGES.seeking).label} · {user.is_premium ? (user.subscription_status === 'trialing' ? 'Pro, free trial' : 'Pro') : 'Free member'}</span></div>
                <button role="menuitem" onClick={() => onNav('word')}><Icon name="book-open" />Daily Word</button>
                {user.is_premium || user.has_billing
                  ? <button role="menuitem" onClick={() => window.dispatchEvent(new CustomEvent('cddd:billing'))}><Icon name="credit-card" />Manage billing</button>
                  : <button role="menuitem" onClick={() => window.dispatchEvent(new CustomEvent('cddd:subscribe', { detail: { plan: 'annual' } }))}><Icon name="credit-card" />Start free trial</button>}
                <button role="menuitem" onClick={() => onNav('signout')}><Icon name="log-out" />Sign out</button>
              </div>
            )}
          </div>
        </div>
      </header>
      {children}
      <nav className="tabbar" aria-label="Primary">
        {tabs.map(([id, l, ic]) => <button key={id} aria-current={cur === id ? 'page' : undefined} onClick={() => onNav(id)}><Icon name={ic} />{l}</button>)}
      </nav>
    </div>
  );
}

function useDailyWord() {
  return useMemo(() => DAILY_WORDS[Math.floor(Date.now() / 86400000) % DAILY_WORDS.length], []);
}

function WordCard({ word, saved, onSave, onOpen }) {
  return (
    <section className="word-card" aria-label="Daily Word">
      <div className="top"><span className="type">{word.type}</span><button className="open" onClick={onOpen}>Daily Word <Icon name="arrow-up-right" /></button></div>
      <blockquote>{word.body}</blockquote>
      <p className="word-ref"><b>{word.reference}</b>· {word.translation}</p>
      {word.reflection && <p className="refl">{word.reflection}</p>}
      <div className="acts">
        <button className="ghost" aria-pressed={saved} onClick={onSave}><Icon name="bookmark" />{saved ? 'Saved' : 'Save'}</button>
        <button className="ghost"><Icon name="share" />Share</button>
      </div>
    </section>
  );
}

function PromptCard({ stage, isPro, onJournal, onSubscribe }) {
  const s = STAGES[stage];
  return (
    <section className="prompt-card" data-s={stage}>
      <span className="lbl"><Icon name={s.icon} />Weekly prompt · {s.label}</span>
      <h3>{WEEKLY_PROMPTS[stage].question}</h3>
      {isPro
        ? <button className="btn btn-ink btn-sm" onClick={onJournal}><Icon name="pen-line" />Answer in your journal</button>
        : <button className="btn btn-ink btn-sm" onClick={() => onSubscribe('annual')}><Icon name="lock" />Unlock with Pro</button>}
    </section>
  );
}

function AppCard() {
  return (
    <section className="app-card">
      <span className="ico"><Icon name="smartphone" /></span>
      <h3>Take it with you</h3>
      <p>The Christian Dating Do's &amp; Don'ts app delivers your daily verse and prayer wall on iOS.</p>
      <div className="note"><b>App and community accounts are separate.</b> Use the same email on both to keep things organized.</div>
      <a className="btn btn-line btn-sm btn-block" href={APP_STORE_URL} target="_blank" rel="noopener noreferrer">Download free app <Icon name="arrow-up-right" /></a>
    </section>
  );
}

function TablePage({ user, isPro, posts, saved, onSave, h, onNav }) {
  const [filter, setFilter] = useState('all');
  const word = useDailyWord();
  const list = filter === 'all' ? posts : posts.filter(p => p.stage === filter);
  return (
    <main className="tbl" data-screen-label={isPro ? '04 The Table (Pro)' : '04 The Table (free member)'}>
      <div className="wrap">
        <div className="tbl-hello">
          <div>
            <p className="tbl-date">{todayStr()}</p>
            <h1 className="display">{greeting()}, Beloved</h1>
            <p className="tbl-stage">You're in <StageBadge stage={user.stage} />. {STAGES[user.stage].note + '.'}</p>
          </div>
        </div>
        <div className="tbl-grid">
          <div className="tbl-word"><WordCard word={word} saved={saved.includes(word.reference)} onSave={() => onSave(word)} onOpen={() => onNav('word')} /></div>
          <section className="tbl-feed" aria-labelledby="tbl-h">
            <div className="feed-head">
              <div><h2 id="tbl-h">The Table</h2><p>Lift each other up. Anonymously.</p></div>
            </div>
            <Filters value={filter} onChange={setFilter} />
            {isPro
              ? <Composer user={user} onSubmit={h.post} />
              : <UpgradeCard onSubscribe={h.subscribe} trialUsed={!!user.subscription_status} />}
            <div className="feed-wrap">
              <div className="feed">
                {list.map(p => (
                  <div key={p.id}>
                    <PostItem post={p} locked={!isPro} onReact={h.react} onReply={h.reply} onReplyReact={h.replyReact} />
                  </div>
                ))}
                {list.length === 0 && (
                  posts.length === 0
                    ? <div className="empty"><Icon name="message-circle" /><h3>The Table is just getting started.</h3><p>{isPro ? 'Be the first to share a prayer or a praise.' : 'Posts from members will appear here.'}</p></div>
                    : <div className="empty"><Icon name="message-circle" /><h3>No posts in this stage yet.</h3><p>Try another stage, or check back soon.</p></div>
                )}
              </div>
            </div>
          </section>
          <aside className="tbl-extras" aria-label="This week">
            <PromptCard stage={user.stage} isPro={isPro} onJournal={() => onNav('journal')} onSubscribe={h.subscribe} />
            <AppCard />
            {isPro && <div className="quiet"><b>Before you open the Table</b><p>Name one person to pray for, then pause.</p></div>}
            <p className="faith">“Not by might, nor by power, but by my Spirit.”<span>(Zechariah 4:6)</span></p>
          </aside>
        </div>
      </div>
    </main>
  );
}

function WordPage({ user, isPro, saved, onSave, onNav, onSubscribe }) {
  const word = useDailyWord();
  const isSaved = saved.includes(word.reference);
  return (
    <main className="reader" data-screen-label="06 Daily Word">
      <div className="wrap">
        <article className="reader-col rise">
          <div className="reader-meta"><span>{todayStr()}</span><span>Daily Word</span></div>
          <div className="reader-plate">
            <span className="type">{word.type}</span>
            <blockquote>{word.body}</blockquote>
            <p className="word-ref"><b>{word.reference}</b>· {word.translation}</p>
          </div>
          {word.reflection && <p className="reader-refl">{word.reflection}</p>}
          <div className="reader-acts">
            <button className="ghost" aria-pressed={isSaved} onClick={() => onSave(word)}><Icon name="bookmark" />{isSaved ? 'Saved' : 'Save'}</button>
            <button className="ghost"><Icon name="share" />Share</button>
          </div>
        </article>
        <div className="reader-next"><PromptCard stage={user.stage} isPro={isPro} onJournal={() => onNav('journal')} onSubscribe={onSubscribe} /></div>
      </div>
    </main>
  );
}

function JournalPage({ user, isPro, entries, onSaveEntry, onNav, onSubscribe }) {
  const [text, setText] = useState('');
  const s = STAGES[user.stage];
  const q = WEEKLY_PROMPTS[user.stage].question;
  return (
    <main className="journal" data-screen-label="07 Journal">
      <div className="wrap"><div className="journal-col rise">
        <button className="back" onClick={() => onNav('table')}><Icon name="arrow-left" />The Table</button>
        <div className="prompt-card" data-s={user.stage} style={{ background: 'none', padding: 0 }}><span className="lbl"><Icon name={s.icon} />Weekly prompt · {s.label}</span></div>
        <h1 className="display">{q}</h1>
        {isPro ? (
          <div className="sheet">
            <label htmlFor="j-text" className="sr-only">Your reflection</label>
            <textarea id="j-text" value={text} onChange={e => setText(e.target.value)} placeholder="Start writing…" />
            <div className="sheet-foot">
              <span>{text.trim() ? text.trim().split(/\s+/).length + ' words' : todayStr()}</span>
              <button className="btn btn-primary btn-sm" disabled={!text.trim()} onClick={() => { onSaveEntry({ q, text: text.trim(), date: todayStr() }); setText(''); }}>Save reflection</button>
            </div>
          </div>
        ) : (
          <UpgradeCard
            onSubscribe={onSubscribe}
            trialUsed={!!user.subscription_status}
            title="Write your reflections"
            body="The weekly prompt and journal are part of Pro."
          />
        )}
        {entries.length > 0 && (
          <section className="entries" aria-label="Your reflections">
            <h2>Your reflections</h2>
            {entries.map((e, i) => <div className="entry" key={i}><time>{e.date}</time><p>{e.text}</p><q>{e.q}</q></div>)}
          </section>
        )}
      </div></div>
    </main>
  );
}

function LibraryPage({ isPro, saved, entries, onNav, onSubscribe }) {
  const [tab, setTab] = useState('saved');
  const words = DAILY_WORDS.filter(w => saved.includes(w.reference));
  return (
    <main className="lib" data-screen-label="08 Library">
      <div className="wrap">
        <h1 className="display">Library</h1>
        <div className="lib-tabs filters" role="tablist">
          <button className="chip" role="tab" aria-selected={tab === 'saved'} aria-pressed={tab === 'saved'} onClick={() => setTab('saved')}><Icon name="bookmark" />Saved words · {words.length}</button>
          <button className="chip" role="tab" aria-selected={tab === 'refl'} aria-pressed={tab === 'refl'} onClick={() => setTab('refl')}><Icon name="pen-line" />Reflections · {entries.length}</button>
        </div>
        {tab === 'saved' && (words.length ? (
          <div className="lib-list">{words.map(w => <article className="lib-item" key={w.reference}><span className="type">{w.type}</span><blockquote>{w.body}</blockquote><div className="row"><p className="word-ref"><b>{w.reference}</b>{w.translation}</p></div></article>)}</div>
        ) : <div className="empty lib-empty"><Icon name="bookmark" /><h3>Nothing saved yet.</h3><p>Tap Save on a Daily Word and it lands here.</p><button className="btn btn-line btn-sm" style={{ marginTop: 18 }} onClick={() => onNav('word')}>Today's reading</button></div>)}
        {tab === 'refl' && (entries.length ? (
          <div className="lib-list">{entries.map((e, i) => <article className="lib-item" key={i}><span className="type" style={{ color: 'var(--text-3)' }}>{e.date}</span><p style={{ fontSize: 16, lineHeight: 1.6 }}>{e.text}</p><p className="word-ref">{e.q}</p></article>)}</div>
        ) : <div className="empty lib-empty"><Icon name="pen-line" /><h3>No reflections yet.</h3><p>{isPro ? 'When you write one, it lands here.' : 'Journaling is part of Pro. Your reflections will land here.'}</p>{isPro
          ? <button className="btn btn-line btn-sm" style={{ marginTop: 18 }} onClick={() => onNav('journal')}>Answer this week's prompt</button>
          : <button className="btn btn-line btn-sm" style={{ marginTop: 18 }} onClick={() => onSubscribe('annual')}><Icon name="lock" />Unlock with Pro</button>}</div>)}
      </div>
    </main>
  );
}

Object.assign(window, { AppShell, TablePage, WordPage, JournalPage, LibraryPage, WordCard, PromptCard, AppCard });
