// home-sections.jsx — homepage: course lists (upcoming/past), leader quotes, articles, global sources

/* ===== courses on the homepage: upcoming + past ===== */
function HomeCourses({ go, t, L, data }) {
  const all = (data && data.courses) || [];
  const upcoming = all.filter(c => (c.term || 'upcoming') === 'upcoming').slice(0, 3);
  const past = all.filter(c => (c.term || 'upcoming') === 'past').slice(0, 3);
  if (!upcoming.length && !past.length) return null;

  const Block = ({ title, sub, items, icon, badge }) => (
    <div style={{ marginTop: 8 }}>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', gap: 20, flexWrap: 'wrap' }}>
        <Reveal><Head eyebrow={t('home_courses_eyebrow')} title={title} sub={sub} icon={icon} /></Reveal>
        <Reveal delay={120}><button className="btn btn-ghost" onClick={() => go('courses')}>{t('see_all')} <Icon.arrow s={16} /></button></Reveal>
      </div>
      <div className="grid g3" style={{ marginTop: 32 }}>
        {items.map((c, i) => (
          <Reveal key={c.id} delay={(i % 3) * 80}><HomeCourseCard c={c} t={t} L={L} badge={badge} onOpen={() => go('courses')} /></Reveal>
        ))}
      </div>
    </div>
  );

  return (
    <section className="section">
      <div className="wrap" style={{ display: 'grid', gap: 64 }}>
        {upcoming.length > 0 && <Block title={t('home_upcoming_title')} sub={t('upcoming_sub')} items={upcoming} icon="rocket" badge="upcoming" />}
        {past.length > 0 && <Block title={t('home_past_title')} sub={t('home_past_sub')} items={past} icon="clock" badge="past" />}
      </div>
    </section>
  );
}

function HomeCourseCard({ c, t, L, badge, onOpen }) {
  const isPast = badge === 'past';
  return (
    <button className="card hoverable home-course" onClick={onOpen}>
      <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap', alignItems: 'center' }}>
        <span className={`badge ${isPast ? 'mint' : 'gold'}`}>
          {isPast ? <><Icon.check s={13} /> {t('completed')}</> : <><Icon.rocket s={13} /> {t('soon')}</>}
        </span>
        <span className="chip" style={{ padding: '4px 10px', fontSize: 12 }}>{L(COURSE_CATS.find(x => x.id === c.cat)?.label)}</span>
      </div>
      <h3 style={{ fontSize: 17.5, marginTop: 14 }}>{L(c.title) || '—'}</h3>
      {c.objective && <p className="muted clamp2" style={{ fontSize: 13.5, lineHeight: 1.6, marginTop: 8 }}>{L(c.objective)}</p>}
      <div className="muted" style={{ fontSize: 13, marginTop: 12, display: 'grid', gap: 5 }}>
        {c.presenter && <span style={{ display: 'flex', gap: 7, alignItems: 'center' }}><Icon.user s={14} /> {L(c.presenter)}</span>}
        {c.date && <span style={{ display: 'flex', gap: 7, alignItems: 'center' }}><Icon.clock s={14} /> {L(c.date)}</span>}
      </div>
      <span className="back-link" style={{ marginTop: 14, color: 'var(--accent)', fontWeight: 700 }}>{t('view_details')} <Icon.arrow s={15} /></span>
    </button>
  );
}

/* ===== leader quotes ===== */
function Inspiration({ t, L }) {
  return (
    <section className="section" style={{ background: 'linear-gradient(180deg, rgba(255,255,255,.02), transparent)' }}>
      <div className="wrap">
        <Reveal><Head center eyebrow={t('insp_eyebrow')} title={t('insp_title')} sub={t('insp_note')} icon="spark" /></Reveal>
        <div className="quote-grid">
          {LEADER_QUOTES.map((q, i) => (
            <Reveal key={i} delay={(i % 3) * 80}>
              <figure className="card quote-card">
                <span className="quote-mark" style={{ color: `var(--${q.accent})` }} aria-hidden="true">
                  <svg width="30" height="22" viewBox="0 0 30 22" fill="currentColor"><path d="M0 22V12C0 5.4 4.2.9 11 0v4.6C7.2 5.4 5.2 7.6 5 11h4.8v11H0Zm19 0V12c0-6.6 4.2-11.1 11-12v4.6c-3.8.8-5.8 3-6 6.4H29v11h-10Z"/></svg>
                </span>
                <blockquote style={{ margin: 0, fontSize: 16.5, lineHeight: 1.7 }}>{L(q.text)}</blockquote>
                <figcaption style={{ marginTop: 18, display: 'flex', alignItems: 'center', gap: 12 }}>
                  <span className="av" style={{ background: `color-mix(in srgb, var(--${q.accent}) 30%, transparent)`, color: 'var(--text)' }}>{L(q.who)[0]}</span>
                  <span>
                    <b style={{ fontSize: 14.5, display: 'block' }}>{L(q.who)}</b>
                    <span className="muted" style={{ fontSize: 12.5 }}>{L(q.role)}{q.paraphrase ? ` · ${t('paraphrase')}` : ''}</span>
                  </span>
                </figcaption>
              </figure>
            </Reveal>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ===== short reads ===== */
function Articles({ t, L }) {
  const [open, setOpen] = React.useState(0);
  return (
    <section className="section">
      <div className="wrap">
        <Reveal><Head eyebrow={t('art_eyebrow')} title={t('art_title')} icon="book" /></Reveal>
        <div className="grid g3" style={{ marginTop: 36 }}>
          {INNOVATION_ARTICLES.map((a, i) => (
            <Reveal key={i} delay={(i % 3) * 80}>
              <article className="card card-pad hoverable" style={{ display: 'flex', flexDirection: 'column', height: '100%' }}>
                <span style={{ width: 44, height: 44, borderRadius: 12, display: 'grid', placeItems: 'center', background: `color-mix(in srgb, var(--${a.accent}) 16%, transparent)`, color: `var(--${a.accent})` }}><Icon.book s={22} /></span>
                <h3 style={{ fontSize: 18, marginTop: 16 }}>{L(a.title)}</h3>
                <p className="muted" style={{ fontSize: 14.5, marginTop: 10, lineHeight: 1.75, display: open === i ? 'block' : '-webkit-box', WebkitLineClamp: 3, WebkitBoxOrient: 'vertical', overflow: 'hidden' }}>{L(a.body)}</p>
                <div style={{ marginTop: 'auto', paddingTop: 14, display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 10 }}>
                  <span className="chip" style={{ padding: '4px 11px', fontSize: 12 }}><Icon.clock s={12} /> {a.mins} {t('min_read')}</span>
                  <button className="btn btn-dark btn-sm" onClick={() => setOpen(open === i ? -1 : i)}>{open === i ? t('cancel') : t('view')}</button>
                </div>
              </article>
            </Reveal>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ===== global innovation windows ===== */
function GlobalSources({ t, L }) {
  return (
    <section className="section" style={{ background: 'linear-gradient(180deg, transparent, rgba(255,255,255,.02))' }}>
      <div className="wrap">
        <Reveal><Head eyebrow={t('src_eyebrow')} title={t('src_title')} icon="globe" /></Reveal>
        <div className="grid g2" style={{ marginTop: 34 }}>
          {GLOBAL_SOURCES.map((s, i) => (
            <Reveal key={i} delay={(i % 2) * 80}>
              <a className="card hoverable src-card" href={s.url} target="_blank" rel="noopener">
                <span style={{ width: 48, height: 48, flex: 'none', borderRadius: 13, display: 'grid', placeItems: 'center', background: `color-mix(in srgb, var(--${s.accent}) 16%, transparent)`, color: `var(--${s.accent})` }}><Icon.globe s={24} /></span>
                <div style={{ flex: 1, minWidth: 0 }}>
                  <b style={{ fontSize: 15.5 }}>{L(s.name)}</b>
                  <p className="muted" style={{ fontSize: 13.5, marginTop: 4, lineHeight: 1.6 }}>{L(s.desc)}</p>
                </div>
                <span className="chip" style={{ flex: 'none', padding: '5px 12px', fontSize: 12.5 }}>{t('visit')} <Icon.arrow s={13} /></span>
              </a>
            </Reveal>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ===== activities accordion: every registered course, expandable for visitors ===== */
function Activities({ go, t, L, data }) {
  const all = (data && data.courses) || [];
  const [open, setOpen] = React.useState(null);
  const [filter, setFilter] = React.useState('all');
  const terms = [
    { id: 'all', label: { ar: 'الكل', en: 'All' } },
    { id: 'upcoming', label: { ar: 'قادمة', en: 'Upcoming' } },
    { id: 'past', label: { ar: 'سابقة', en: 'Past' } },
  ];
  const list = all.filter(c => filter === 'all' || (c.term || 'upcoming') === filter);
  const badgeOf = (term) => term === 'past' ? { cls: 'mint', ic: 'check', key: 'completed' } : { cls: 'gold', ic: 'rocket', key: 'soon' };

  return (
    <section className="section" id="activities">
      <div className="wrap">
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', gap: 20, flexWrap: 'wrap' }}>
          <Reveal><Head eyebrow={t('activities_eyebrow')} title={t('activities_title')} sub={t('activities_sub')} icon="layers" /></Reveal>
          <Reveal delay={120}><button className="btn btn-ghost" onClick={() => go('courses')}>{t('see_all')} <Icon.arrow s={16} /></button></Reveal>
        </div>

        {all.length === 0 ? (
          <Reveal><p className="muted center" style={{ padding: '34px 0', fontSize: 15 }}>{t('activities_empty')}</p></Reveal>
        ) : (
          <>
            <Reveal>
              <div className="tabs" style={{ marginTop: 26 }}>
                {terms.map(x => (
                  <button key={x.id} className={`tab ${filter === x.id ? 'on' : ''}`} onClick={() => { setFilter(x.id); setOpen(null); }}>
                    {L(x.label)} · {x.id === 'all' ? all.length : all.filter(c => (c.term || 'upcoming') === x.id).length}
                  </button>
                ))}
              </div>
            </Reveal>
            <div style={{ display: 'grid', gap: 10, marginTop: 22 }}>
              {list.map((c, i) => {
                const term = c.term || 'upcoming';
                const b = badgeOf(term);
                const B = Icon[b.ic];
                const isOpen = open === c.id;
                const files = c.files || [];
                return (
                  <Reveal key={c.id} delay={Math.min(i, 5) * 50}>
                    <div className={`card act-row ${isOpen ? 'open' : ''}`}>
                      <button className="act-head" onClick={() => setOpen(isOpen ? null : c.id)} aria-expanded={isOpen}>
                        <span className={`badge ${b.cls}`} style={{ flex: 'none' }}><B s={13} /> {t(b.key)}</span>
                        <span style={{ flex: 1, minWidth: 0, textAlign: 'start' }}>
                          <b style={{ fontSize: 15.5, display: 'block' }}>{L(c.title) || '—'}</b>
                          <span className="muted" style={{ fontSize: 12.5 }}>
                            {L(c.presenter)}{c.date ? ` · ${L(c.date)}` : ''}{files.length ? ` · ${files.length} ${t('attachments')}` : ''}
                          </span>
                        </span>
                        <span className="act-chev" style={{ flex: 'none', color: 'var(--accent)' }}><Icon.chevR s={18} /></span>
                      </button>
                      {isOpen && (
                        <div className="act-body">
                          {c.objective && <p style={{ fontSize: 14.5, lineHeight: 1.75, margin: 0 }}>{L(c.objective)}</p>}
                          <div className="grid g2" style={{ gap: 14, marginTop: 16 }}>
                            {c.presenter && <MetaRow icon="user" label={t('presenter')} value={L(c.presenter)} />}
                            {c.date && <MetaRow icon="clock" label={term === 'past' ? t('held_on') : t('course_date')} value={L(c.date)} />}
                            {c.dur && <MetaRow icon="clock" label={t('duration')} value={L(c.dur)} />}
                            {c.mode && <MetaRow icon={c.mode === 'onsite' ? 'pin' : 'globe'} label={L({ ar: 'النمط', en: 'Mode' })} value={c.mode === 'onsite' ? t('mode_onsite') : t('mode_virtual')} />}
                            {c.target !== '' && c.target != null && <MetaRow icon="users" label={t('target_emp')} value={c.target} />}
                          </div>
                          {c.outcomes && (
                            <div style={{ marginTop: 16 }}>
                              <div className="eyebrow"><Icon.target s={14} /> {t('outcomes')}</div>
                              <p style={{ fontSize: 14.5, marginTop: 8, lineHeight: 1.7 }}>{L(c.outcomes)}</p>
                            </div>
                          )}
                          {files.length > 0 && (
                            <div style={{ marginTop: 16 }}>
                              <div className="eyebrow"><Icon.layers s={14} /> {t('attachments')} · {files.length}</div>
                              <div style={{ marginTop: 10 }}><AttachList items={files} L={L} /></div>
                            </div>
                          )}
                          {term !== 'past' && (
                            <button className="btn btn-primary btn-sm" style={{ marginTop: 18 }} onClick={() => go('courses')}>
                              <Icon.arrow s={15} /> {t('reg_now')}
                            </button>
                          )}
                        </div>
                      )}
                    </div>
                  </Reveal>
                );
              })}
            </div>
          </>
        )}
      </div>
    </section>
  );
}

Object.assign(window, { HomeCourses, HomeCourseCard, Inspiration, Articles, GlobalSources, Activities });
