/* ============================================================
   TechManager — PUBLIC PAGES (landing, catalog, roadmap)
   ============================================================ */

/* ---------- Reusable cert card ---------- */
const CertCard = ({ cert, onOpen }) => {
  const soon = cert.status === 'COMING_SOON';
  return (
    <div className="card card-hover" style={{ padding: 24, display: 'flex', flexDirection: 'column', gap: 16, opacity: soon ? 0.92 : 1 }}>
      <div className="between">
        <Orb cert={cert} size={62} />
        <StatusBadge status={cert.status} />
      </div>
      <div>
        <h3 className="display" style={{ fontSize: 22 }}>{cert.name}</h3>
        <p className="muted" style={{ fontSize: 14.5, margin: '8px 0 0' }}>{cert.description}</p>
      </div>
      <div className="flex gap-8" style={{ flexWrap: 'wrap' }}>
        {cert.areas.slice(0, 3).map((a) => <span key={a} className="chip" style={{ fontSize: 12, padding: '5px 10px' }}>{a}</span>)}
        {cert.areas.length > 3 && <span className="chip" style={{ fontSize: 12, padding: '5px 10px' }}>+{cert.areas.length - 3}</span>}
      </div>
      <div className="between" style={{ marginTop: 'auto', paddingTop: 6 }}>
        <div className="flex gap-16" style={{ fontSize: 13 }}>
          <span className="muted center gap-8"><Icon name="book" size={15} /> {cert.modules.length || '—'} modules</span>
          <span className="muted center gap-8"><Icon name="target" size={15} /> ${cert.price}</span>
        </div>
        {soon
          ? <span className="btn btn-soft btn-sm" style={{ pointerEvents: 'none' }}>Notify me</span>
          : <button className="btn btn-primary btn-sm" onClick={() => onOpen(cert)}>Start path <Icon name="arrow" size={15} /></button>}
      </div>
    </div>
  );
};

/* ---------- Credibility block (used on landing + standalone) ---------- */
const CredibilitySection = ({ db }) => (
  <section className="section" style={{ background: 'var(--bg-soft)', borderTop: '1px solid var(--border)', borderBottom: '1px solid var(--border)' }}>
    <div className="wrap">
      <div style={{ textAlign: 'center', marginBottom: 48 }}>
        <div className="eyebrow">The evidence</div>
        <h2 className="display" style={{ marginTop: 14 }}>Credibility you can measure</h2>
      </div>
      <div className="grid" style={{ gridTemplateColumns: 'repeat(auto-fit, minmax(240px, 1fr))', gap: 18, marginBottom: 40 }}>
        {db.credibility.evidence.map((e) => (
          <div key={e.id} className="card" style={{ padding: 28, textAlign: 'center' }}>
            <div className="display text-grad" style={{ fontSize: 52 }}>{e.stat}</div>
            <p style={{ margin: '6px auto 10px', maxWidth: '28ch', fontSize: 15 }}>{e.label}</p>
            <div className="dim" style={{ fontSize: 12 }}>{e.source}</div>
          </div>
        ))}
      </div>
      <div className="grid" style={{ gridTemplateColumns: 'repeat(auto-fit, minmax(280px, 1fr))', gap: 18 }}>
        {db.credibility.feedback.map((f) => (
          <figure key={f.id} className="card" style={{ padding: 26, margin: 0 }}>
            <div className="text-grad" style={{ fontSize: 40, lineHeight: 0.5, fontFamily: 'Georgia, serif', height: 22 }}>“</div>
            <blockquote style={{ margin: '8px 0 18px', fontSize: 16.5, lineHeight: 1.5 }}>{f.quote}</blockquote>
            <figcaption className="center gap-12">
              <div style={{ width: 38, height: 38, borderRadius: '50%', background: 'linear-gradient(135deg, var(--blue-2), var(--blue-deep))', display: 'grid', placeItems: 'center', fontWeight: 800, fontSize: 14 }}>{f.name[0]}</div>
              <div><div style={{ fontWeight: 700, fontSize: 14 }}>{f.name}</div><div className="dim" style={{ fontSize: 12.5 }}>{f.role}</div></div>
            </figcaption>
          </figure>
        ))}
      </div>
    </div>
  </section>
);

/* ---------- Roadmap timeline ---------- */
const RoadmapTimeline = ({ db }) => {
  const items = TM.sortedRoadmap();
  return (
    <div style={{ position: 'relative', maxWidth: 760, margin: '0 auto' }}>
      <div style={{ position: 'absolute', left: 19, top: 8, bottom: 8, width: 2, background: 'linear-gradient(var(--blue), transparent)' }} />
      <div className="grid" style={{ gap: 16 }}>
        {items.map((r) => (
          <div key={r.id} className="flex gap-16" style={{ position: 'relative' }}>
            <div style={{ width: 40, flex: '0 0 40px', display: 'grid', placeItems: 'center', paddingTop: 22 }}>
              <span style={{ width: 14, height: 14, borderRadius: '50%', background: r.exact ? 'var(--blue)' : 'var(--panel-3)', border: '2px solid var(--blue-2)', boxShadow: '0 0 12px var(--blue-glow)' }} />
            </div>
            <div className="card" style={{ padding: 20, flex: 1 }}>
              <div className="between" style={{ flexWrap: 'wrap', gap: 8 }}>
                <h3 className="display" style={{ fontSize: 20 }}>{r.title}</h3>
                <span className={'chip'} style={{ borderColor: r.exact ? 'rgba(47,125,246,0.4)' : 'var(--border-strong)', color: r.exact ? 'var(--blue-2)' : 'var(--muted)' }}>
                  <Icon name="clock" size={14} /> {r.date}
                </span>
              </div>
              <p className="muted" style={{ margin: '8px 0 0', fontSize: 14.5 }}>{r.description}</p>
            </div>
          </div>
        ))}
      </div>
    </div>
  );
};

/* ---------- LANDING ---------- */
const Landing = ({ db, go, openCert }) => {
  const visible = TM.visibleCerts();
  const problems = [
    { n: '01', t: 'Certifications ignore management', d: 'Traditional PM and leadership certs teach process and frameworks — but nothing about the technical systems your team builds every day.' },
    { n: '02', t: 'Engineering teams are deeply technical', d: 'Managers work alongside engineers in cloud infrastructure, data pipelines, CI/CD systems, and AI tooling. Every single day.' },
    { n: '03', t: 'A credibility gap emerges', d: "Without technical context, managers can't ask the right questions, can't spot risk, and lose authority in technical conversations." },
    { n: '04', t: 'TechManager closes that gap', d: 'Not to turn you into an engineer — but to make you a more credible, confident, and effective leader.' },
  ];
  return (
    <div className="fadein">
      {/* hero */}
      <section className="wrap" style={{ paddingTop: 70, paddingBottom: 40 }}>
        <div className="grid" style={{ gridTemplateColumns: '1.05fr 0.95fr', gap: 40, alignItems: 'center' }}>
          <div>
            <span className="chip"><Icon name="sparkle" size={15} style={{ color: 'var(--blue-2)' }} /> Stackable credentials for managers</span>
            <h1 className="display" style={{ marginTop: 22 }}>
              Does your engineering team <span className="text-grad">speak a language you don't understand?</span>
            </h1>
            <p className="lede" style={{ marginTop: 22 }}>
              TechManager gives you the vocabulary, the right questions, and the decision-making criteria to lead engineering teams with credibility. No coding required.
            </p>
            <div className="flex gap-12" style={{ marginTop: 30, flexWrap: 'wrap' }}>
              <button className="btn btn-primary" onClick={() => go('certs')}>Discover your technical gaps <Icon name="arrow" /></button>
              <button className="btn btn-ghost" onClick={() => go('certs')}>Explore modules</button>
            </div>
            <p className="muted" style={{ marginTop: 22, fontSize: 14.5 }}>
              Join <strong style={{ color: 'var(--text)' }}>{TM.managersCertified().toLocaleString()}+ managers</strong> already leading with confidence
            </p>
          </div>
          <div>
            <NodeGraph certs={visible} onSelect={(c) => c.status === 'READY' ? openCert(c) : null} />
          </div>
        </div>
      </section>

      {/* stats */}
      <div className="statbar">
        <div className="wrap"><div className="statgrid">
          <div className="stat"><div className="stat-num">{TM.managersCertified().toLocaleString()}+</div><div className="stat-label">Managers Certified</div></div>
          <div className="stat"><div className="stat-num">{TM.domainsCovered()}</div><div className="stat-label">Domains Covered</div></div>
          <div className="stat"><div className="stat-num">${db.stats.pricePerCertLow}–${db.stats.pricePerCertHigh}</div><div className="stat-label">Per Certification</div></div>
          <div className="stat"><div className="stat-num">Lifetime</div><div className="stat-label">Credential Validity</div></div>
        </div></div>
      </div>

      {/* problem */}
      <section className="section">
        <div className="wrap">
          <div style={{ textAlign: 'center', marginBottom: 50 }}>
            <div className="eyebrow">The problem</div>
            <h2 className="display" style={{ marginTop: 14 }}>The credibility gap is real</h2>
          </div>
          <div className="grid" style={{ gridTemplateColumns: 'repeat(auto-fit, minmax(320px, 1fr))', gap: 20 }}>
            {problems.map((p) => (
              <div key={p.n} className="card" style={{ padding: 30 }}>
                <div className="display" style={{ fontSize: 30, color: 'var(--blue-2)' }}>{p.n}</div>
                <h3 className="display" style={{ fontSize: 21, marginTop: 16 }}>{p.t}</h3>
                <p className="muted" style={{ marginTop: 10, fontSize: 14.5 }}>{p.d}</p>
              </div>
            ))}
          </div>
        </div>
      </section>

      {/* certification path */}
      <section className="section tight" style={{ background: 'var(--bg-soft)', borderTop: '1px solid var(--border)' }}>
        <div className="wrap">
          <div className="between" style={{ marginBottom: 36, flexWrap: 'wrap', gap: 16 }}>
            <div>
              <div className="eyebrow">The certification path</div>
              <h2 className="display" style={{ marginTop: 14 }}>Stack credibility, one domain at a time</h2>
            </div>
            <button className="btn btn-ghost" onClick={() => go('certs')}>View all certifications <Icon name="arrow" /></button>
          </div>
          <div className="grid" style={{ gridTemplateColumns: 'repeat(auto-fit, minmax(290px, 1fr))', gap: 20 }}>
            {visible.slice(0, 3).map((c) => <CertCard key={c.id} cert={c} onOpen={openCert} />)}
          </div>
        </div>
      </section>

      <CredibilitySection db={db} />

      {/* roadmap preview */}
      <section className="section">
        <div className="wrap">
          <div style={{ textAlign: 'center', marginBottom: 48 }}>
            <div className="eyebrow">What's next</div>
            <h2 className="display" style={{ marginTop: 14 }}>The roadmap</h2>
          </div>
          <RoadmapTimeline db={db} />
        </div>
      </section>

      {/* CTA */}
      <section className="section tight">
        <div className="wrap">
          <div className="card" style={{ padding: '56px 40px', textAlign: 'center', background: 'radial-gradient(700px 300px at 50% -40%, rgba(47,125,246,0.22), transparent), var(--panel)' }}>
            <h2 className="display" style={{ fontSize: 'clamp(28px,3.4vw,42px)' }}>Lead with credibility. Start free.</h2>
            <p className="lede" style={{ margin: '16px auto 0' }}>Pick a domain, study the modules, pass the exam, and earn a lifetime credential you can share on LinkedIn.</p>
            <div className="flex gap-12" style={{ justifyContent: 'center', marginTop: 28 }}>
              <button className="btn btn-primary" onClick={() => go('certs')}>Browse certifications <Icon name="arrow" /></button>
              <button className="btn btn-ghost" onClick={() => go('profile')}>View my profile</button>
            </div>
          </div>
        </div>
      </section>
    </div>
  );
};

/* ---------- CERTIFICATION CATALOG ---------- */
const CertCatalog = ({ db, go, openCert }) => {
  const visible = TM.visibleCerts(); // READY + COMING_SOON only; NOT_READY hidden
  return (
    <div className="fadein wrap section tight">
      <div style={{ marginBottom: 14 }}>
        <div className="eyebrow">Certification path</div>
        <h1 className="display" style={{ fontSize: 'clamp(34px,4vw,52px)', marginTop: 14 }}>Choose your next credential</h1>
        <p className="lede" style={{ marginTop: 14 }}>Every certification is a stackable, lifetime credential. Click one to open its learning path and study the modules before you sit the exam.</p>
      </div>

      {/* map of how they connect */}
      <div className="card" style={{ padding: 24, margin: '28px 0 36px' }}>
        <div className="between" style={{ marginBottom: 8 }}>
          <span className="field-label" style={{ margin: 0 }}>How the certifications connect</span>
          <span className="dim" style={{ fontSize: 12.5 }}>Configured in the admin panel</span>
        </div>
        <div style={{ maxWidth: 560, margin: '0 auto' }}>
          <NodeGraph certs={visible} orbSize={74} onSelect={(c) => c.status === 'READY' ? openCert(c) : null} />
        </div>
      </div>

      <div className="grid" style={{ gridTemplateColumns: 'repeat(auto-fill, minmax(300px, 1fr))', gap: 20 }}>
        {visible.map((c) => <CertCard key={c.id} cert={c} onOpen={openCert} />)}
      </div>
    </div>
  );
};

/* ---------- ROADMAP PAGE ---------- */
const RoadmapPage = ({ db }) => (
  <div className="fadein wrap section tight">
    <div style={{ textAlign: 'center', marginBottom: 48 }}>
      <div className="eyebrow">Product roadmap</div>
      <h1 className="display" style={{ fontSize: 'clamp(34px,4vw,52px)', marginTop: 14 }}>Where TechManager is headed</h1>
      <p className="lede" style={{ margin: '14px auto 0' }}>New domains and features, ordered by what's coming next. Dates are firm where shown, otherwise "coming soon."</p>
    </div>
    <RoadmapTimeline db={db} />
  </div>
);

Object.assign(window, { CertCard, CredibilitySection, RoadmapTimeline, Landing, CertCatalog, RoadmapPage });
