// Caliber — Interactive demos for /ai-principles.
// Three small demos that prove the items in the "What AI does" list:
//   1) Scout search (imports ScoutDemo from product-mocks.jsx)
//   2) JD draft — role brief → animated JD text
//   3) CV parsing — CV card → candidate record fields populating
//
// Each demo is self-contained, no network calls, pre-baked data. They
// respect the existing typing/fade-in idioms used elsewhere in the codebase.
//
// Exports: <AIPDemos />

const AIPD_MONO = 'ui-monospace, "SF Mono", Menlo, Consolas, monospace';

/* ══════════════════════════════════════════════════════════════
   JD DRAFT DEMO
   ══════════════════════════════════════════════════════════════ */

const JD_ROLE = 'Senior React Engineer';
const JD_MUSTHAVES = ['React', 'TypeScript', '5+ years', 'Auckland'];
const JD_LINES = [
  { kind: 'h', text: 'About the role' },
  { kind: 'p', text: "We're a Series-A Auckland fintech building tools used by 40,000 recruiters across Aotearoa and Australia. We're looking for a Senior React Engineer to join the product team." },
  { kind: 'h', text: "What you'll do" },
  { kind: 'li', text: 'Build user-facing features in React and TypeScript' },
  { kind: 'li', text: 'Partner with design and product to ship well-crafted UI' },
  { kind: 'li', text: 'Own a shared component library used across the product' },
  { kind: 'h', text: "What we're looking for" },
  { kind: 'li', text: '5+ years in a senior front-end role' },
  { kind: 'more', text: '+ 12 more lines — responsibilities, benefits, how to apply. You edit anything before it ships.' },
];

function JDDraftDemo() {
  const [phase, setPhase] = React.useState('idle'); // idle | writing | done
  const [revealed, setRevealed] = React.useState(0);

  const start = React.useCallback(() => {
    if (phase !== 'idle') return;
    setPhase('writing');
    setRevealed(0);
    let i = 0;
    const tick = () => {
      i += 1;
      setRevealed(i);
      if (i < JD_LINES.length) {
        setTimeout(tick, 260);
      } else {
        setPhase('done');
      }
    };
    setTimeout(tick, 320);
  }, [phase]);

  const reset = () => { setPhase('idle'); setRevealed(0); };

  return (
    <div style={{
      background: '#fff',
      border: '1px solid var(--cal-line)',
      borderRadius: 20,
      padding: '24px 24px 20px',
      display: 'flex', flexDirection: 'column', gap: 16,
      boxShadow: 'var(--shadow-sm)',
      height: '100%',
    }}>
      {/* Header */}
      <div style={{
        display: 'flex', alignItems: 'center', gap: 8,
        fontSize: 11, letterSpacing: '0.14em', textTransform: 'uppercase',
        color: 'var(--cal-muted)', fontFamily: AIPD_MONO, fontWeight: 600,
      }}>
        <CalStar size={11} grad={false} style={{ color: 'var(--cal-blue-ink)' }} />
        Draft a job advert
      </div>

      {/* Inputs */}
      <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
        <div>
          <div style={{ fontSize: 10, color: 'var(--cal-muted-2)', letterSpacing: '0.12em', fontFamily: AIPD_MONO, fontWeight: 500, marginBottom: 6, textTransform: 'uppercase' }}>Role</div>
          <div style={{
            padding: '10px 14px', borderRadius: 10,
            background: 'var(--cal-bg-soft)', border: '1px solid var(--cal-line-2)',
            fontSize: 14, fontWeight: 500, color: 'var(--cal-ink)',
          }}>{JD_ROLE}</div>
        </div>
        <div>
          <div style={{ fontSize: 10, color: 'var(--cal-muted-2)', letterSpacing: '0.12em', fontFamily: AIPD_MONO, fontWeight: 500, marginBottom: 6, textTransform: 'uppercase' }}>Must haves</div>
          <div style={{ display: 'flex', gap: 6, flexWrap: 'wrap' }}>
            {JD_MUSTHAVES.map(m => (
              <span key={m} style={{
                padding: '6px 10px', borderRadius: 999,
                background: 'var(--cal-blue-50)', color: 'var(--cal-blue-ink)',
                fontSize: 12, fontWeight: 500,
                border: '1px solid rgba(0,9,182,0.14)',
              }}>{m}</span>
            ))}
          </div>
        </div>
      </div>

      {/* CTA / state */}
      {phase === 'idle' ? (
        <button onClick={start} className="cal-btn cal-btn--blue" style={{ padding: '10px 16px', fontSize: 13, alignSelf: 'flex-start' }}>
          Draft the advert {Icon.arrowR({ size: 12, color: '#fff' })}
        </button>
      ) : (
        <div style={{
          display: 'flex', alignItems: 'center', gap: 8,
          fontSize: 11, color: 'var(--cal-muted)',
          fontFamily: AIPD_MONO, letterSpacing: '0.06em',
        }}>
          {phase === 'writing' ? (
            <>
              <span style={{
                width: 10, height: 10, borderRadius: 999,
                border: '2px solid rgba(0,9,182,0.18)', borderTopColor: 'var(--cal-blue-ink)',
                animation: 'cal-star-spin 0.8s linear infinite',
              }} />
              Drafting…
            </>
          ) : (
            <>
              <span style={{ width: 7, height: 7, borderRadius: 999, background: '#16A34A' }} />
              Draft ready — you edit before it goes live.
              <button onClick={reset} style={{
                marginLeft: 'auto', background: 'transparent', border: 'none',
                color: 'var(--cal-blue-ink)', cursor: 'pointer', fontSize: 11,
                fontFamily: AIPD_MONO, letterSpacing: '0.06em', textTransform: 'uppercase',
              }}>Reset</button>
            </>
          )}
        </div>
      )}

      {/* Output — fixed min-height so page doesn't grow as lines animate in */}
      <div style={{
        background: 'var(--cal-bg-soft)',
        border: '1px solid var(--cal-line-2)',
        borderRadius: 12,
        padding: '18px 20px',
        minHeight: 380,
        fontSize: 13.5, lineHeight: 1.55, color: 'var(--cal-body)',
        display: 'flex', flexDirection: 'column', gap: 8,
        position: 'relative',
      }}>
        {phase === 'idle' ? (
          // Idle state — faded preview of first 3 lines so the box isn't empty
          <div style={{ opacity: 0.35, display: 'flex', flexDirection: 'column', gap: 8 }}>
            <div style={{ fontWeight: 600, color: 'var(--cal-ink)' }}>About the role</div>
            <div>A modern platform used by 40,000 recruiters across Aotearoa and Australia…</div>
            <div style={{ fontWeight: 600, color: 'var(--cal-ink)', marginTop: 6 }}>What you'll do</div>
            <div style={{ display: 'flex', gap: 10 }}>
              <span style={{ color: 'var(--cal-muted-2)' }}>–</span>
              <span>Build user-facing features in React and TypeScript</span>
            </div>
            <div style={{
              marginTop: 'auto', paddingTop: 12,
              fontStyle: 'italic', fontSize: 12, color: 'var(--cal-muted-2)',
              textAlign: 'center',
            }}>
              Click "Draft the advert" to generate a real first draft.
            </div>
          </div>
        ) : (
          <>
            {JD_LINES.slice(0, revealed).map((ln, i) => (
              ln.kind === 'h' ? (
                <div key={i} style={{
                  fontWeight: 600, color: 'var(--cal-ink)',
                  fontSize: 13.5, marginTop: i === 0 ? 0 : 6,
                  animation: 'cal-fade-up 0.3s ease',
                }}>{ln.text}</div>
              ) : ln.kind === 'li' ? (
                <div key={i} style={{
                  display: 'flex', gap: 10,
                  animation: 'cal-fade-up 0.3s ease',
                }}>
                  <span style={{ color: 'var(--cal-muted-2)' }}>–</span>
                  <span>{ln.text}</span>
                </div>
              ) : ln.kind === 'more' ? (
                <div key={i} style={{
                  marginTop: 'auto', paddingTop: 10,
                  borderTop: '1px dashed var(--cal-line)',
                  fontSize: 11.5, fontFamily: AIPD_MONO,
                  color: 'var(--cal-muted-2)', letterSpacing: '0.03em',
                  animation: 'cal-fade-up 0.3s ease',
                }}>{ln.text}</div>
              ) : (
                <div key={i} style={{ animation: 'cal-fade-up 0.3s ease' }}>{ln.text}</div>
              )
            ))}
            {phase === 'writing' && (
              <span aria-hidden className="cal-blink" style={{
                color: 'var(--cal-blue-ink)', fontWeight: 600,
                marginLeft: 2,
              }}>▍</span>
            )}
          </>
        )}
      </div>
    </div>
  );
}

/* ══════════════════════════════════════════════════════════════
   CV PARSING DEMO
   ══════════════════════════════════════════════════════════════ */

const CV_FIELDS = [
  { label: 'Name',          value: 'Kate Morrison' },
  { label: 'Current role',  value: 'Senior Software Engineer' },
  { label: 'Employer',      value: 'Xero' },
  { label: 'Location',      value: 'Wellington, New Zealand' },
  { label: 'Seniority',     value: 'Senior (8 yrs)' },
  { label: 'Skills',        value: 'React · TypeScript · Node.js · PostgreSQL' },
];

function CVParsingDemo() {
  const [phase, setPhase] = React.useState('idle');
  const [filled, setFilled] = React.useState(0);

  const start = React.useCallback(() => {
    if (phase !== 'idle') return;
    setPhase('parsing');
    setFilled(0);
    let i = 0;
    const tick = () => {
      i += 1;
      setFilled(i);
      if (i < CV_FIELDS.length) {
        setTimeout(tick, 220);
      } else {
        setPhase('done');
      }
    };
    setTimeout(tick, 400);
  }, [phase]);

  const reset = () => { setPhase('idle'); setFilled(0); };

  return (
    <div style={{
      background: '#fff',
      border: '1px solid var(--cal-line)',
      borderRadius: 20,
      padding: '24px 24px 20px',
      display: 'flex', flexDirection: 'column', gap: 16,
      boxShadow: 'var(--shadow-sm)',
      height: '100%',
    }}>
      {/* Header */}
      <div style={{
        display: 'flex', alignItems: 'center', gap: 8,
        fontSize: 11, letterSpacing: '0.14em', textTransform: 'uppercase',
        color: 'var(--cal-muted)', fontFamily: AIPD_MONO, fontWeight: 600,
      }}>
        <CalStar size={11} grad={false} style={{ color: 'var(--cal-blue-ink)' }} />
        Parse a CV
      </div>

      {/* Split: CV preview | field panel */}
      <div style={{
        display: 'grid', gridTemplateColumns: '0.9fr 1.1fr', gap: 14,
        flex: 1,
      }}>
        {/* CV card */}
        <div style={{
          background: '#FDFCF8',
          border: '1px solid var(--cal-line-2)',
          borderRadius: 10,
          padding: '18px 16px',
          display: 'flex', flexDirection: 'column', gap: 8,
          fontSize: 10, color: 'var(--cal-body)', lineHeight: 1.45,
          position: 'relative',
        }}>
          <div style={{
            position: 'absolute', top: 10, right: 10,
            fontSize: 9, letterSpacing: '0.1em', textTransform: 'uppercase',
            color: 'var(--cal-muted-2)', fontFamily: AIPD_MONO,
          }}>kate-morrison.pdf</div>
          <div style={{ fontSize: 14, fontWeight: 600, color: 'var(--cal-ink)', letterSpacing: '-0.01em' }}>
            Kate Morrison
          </div>
          <div style={{ fontSize: 10, color: 'var(--cal-muted)' }}>
            kate.morrison@example.com · 021 400 400 · Wellington, NZ
          </div>
          <div style={{ height: 1, background: 'var(--cal-line-2)', margin: '4px 0' }} />
          <div style={{ fontSize: 10, fontWeight: 600, color: 'var(--cal-ink)' }}>Experience</div>
          <div>
            <div style={{ fontWeight: 500, color: 'var(--cal-ink)' }}>Senior Software Engineer, Xero</div>
            <div style={{ color: 'var(--cal-muted)' }}>2019 – present · Wellington</div>
            <div style={{ marginTop: 2 }}>React + TypeScript frontend. Design system. Shared component library.</div>
          </div>
          <div>
            <div style={{ fontWeight: 500, color: 'var(--cal-ink)' }}>Full Stack Engineer, Pushpay</div>
            <div style={{ color: 'var(--cal-muted)' }}>2016 – 2019 · Auckland</div>
          </div>
          <div>
            <div style={{ fontWeight: 500, color: 'var(--cal-ink)' }}>Graduate Developer, Catalyst IT</div>
            <div style={{ color: 'var(--cal-muted)' }}>2014 – 2016 · Wellington</div>
          </div>
          <div style={{ height: 1, background: 'var(--cal-line-2)', margin: '4px 0' }} />
          <div style={{ fontSize: 10, fontWeight: 600, color: 'var(--cal-ink)' }}>Skills</div>
          <div>React · TypeScript · Node.js · PostgreSQL · GraphQL</div>
        </div>

        {/* Field panel */}
        <div style={{
          background: 'var(--cal-bg-soft)',
          border: '1px solid var(--cal-line-2)',
          borderRadius: 10,
          padding: '16px 16px',
          display: 'flex', flexDirection: 'column', gap: 10,
          minHeight: 260,
        }}>
          <div style={{
            fontSize: 10, fontWeight: 600, color: 'var(--cal-ink)',
            letterSpacing: '0.08em', textTransform: 'uppercase',
            fontFamily: AIPD_MONO,
            display: 'flex', alignItems: 'center', gap: 6,
          }}>
            <span>Candidate record</span>
            <span style={{ fontFamily: 'var(--cal-font)', fontWeight: 500, color: 'var(--cal-muted-2)', letterSpacing: 0, textTransform: 'none', fontSize: 10 }}>
              {filled}/{CV_FIELDS.length}
            </span>
          </div>
          {CV_FIELDS.map((f, i) => {
            const isFilled = i < filled;
            return (
              <div key={f.label} style={{
                display: 'flex', flexDirection: 'column', gap: 2,
                opacity: isFilled ? 1 : 0.4,
                transition: 'opacity .25s ease',
              }}>
                <div style={{
                  fontSize: 9.5, color: 'var(--cal-muted-2)',
                  letterSpacing: '0.1em', textTransform: 'uppercase',
                  fontFamily: AIPD_MONO, fontWeight: 500,
                }}>{f.label}</div>
                <div style={{
                  fontSize: 12.5, fontWeight: isFilled ? 500 : 400,
                  color: isFilled ? 'var(--cal-ink)' : 'var(--cal-muted-2)',
                  padding: '6px 10px',
                  background: '#fff',
                  border: '1px solid var(--cal-line-2)',
                  borderRadius: 6,
                  fontFamily: f.label === 'Name' ? 'var(--cal-font)' : 'var(--cal-font)',
                  animation: isFilled ? 'cal-fade-up 0.25s ease' : 'none',
                  minHeight: 18,
                }}>
                  {isFilled ? f.value : '—'}
                </div>
              </div>
            );
          })}
        </div>
      </div>

      {/* CTA / state */}
      {phase === 'idle' ? (
        <button onClick={start} className="cal-btn cal-btn--blue" style={{ padding: '10px 16px', fontSize: 13, alignSelf: 'flex-start' }}>
          Parse the CV {Icon.arrowR({ size: 12, color: '#fff' })}
        </button>
      ) : (
        <div style={{
          display: 'flex', alignItems: 'center', gap: 8,
          fontSize: 11, color: 'var(--cal-muted)',
          fontFamily: AIPD_MONO, letterSpacing: '0.06em',
        }}>
          {phase === 'parsing' ? (
            <>
              <span style={{
                width: 10, height: 10, borderRadius: 999,
                border: '2px solid rgba(0,9,182,0.18)', borderTopColor: 'var(--cal-blue-ink)',
                animation: 'cal-star-spin 0.8s linear infinite',
              }} />
              Reading CV…
            </>
          ) : (
            <>
              <span style={{ width: 7, height: 7, borderRadius: 999, background: '#16A34A' }} />
              Record ready — you check the fields and save.
              <button onClick={reset} style={{
                marginLeft: 'auto', background: 'transparent', border: 'none',
                color: 'var(--cal-blue-ink)', cursor: 'pointer', fontSize: 11,
                fontFamily: AIPD_MONO, letterSpacing: '0.06em', textTransform: 'uppercase',
              }}>Reset</button>
            </>
          )}
        </div>
      )}
    </div>
  );
}

/* ══════════════════════════════════════════════════════════════
   SCOUT DEMO WRAPPER — reuses <ScoutDemo /> from product-mocks.jsx,
   wraps it in the same framed-card vocabulary as the other two demos.
   ══════════════════════════════════════════════════════════════ */
function ScoutDemoFrame() {
  return (
    <div style={{
      background: '#fff',
      border: '1px solid var(--cal-line)',
      borderRadius: 20,
      padding: '24px 24px 24px',
      boxShadow: 'var(--shadow-sm)',
      display: 'flex', flexDirection: 'column', gap: 16,
    }}>
      <div style={{
        display: 'flex', alignItems: 'center', gap: 8,
        fontSize: 11, letterSpacing: '0.14em', textTransform: 'uppercase',
        color: 'var(--cal-muted)', fontFamily: AIPD_MONO, fontWeight: 600,
      }}>
        <CalStar size={11} grad={false} style={{ color: 'var(--cal-blue-ink)' }} />
        Brief Scout in plain English
      </div>

      <div style={{
        fontSize: 14, lineHeight: 1.5, color: 'var(--cal-body)', maxWidth: 780,
      }}>
        Watch Scout turn a plain-English brief into a structured search across 2.4&nbsp;billion public profiles — no ranking, no fit score, no black box.
        The filter chips show you exactly what it searched for. The candidate list is ordered by profile completeness, not by a model's opinion.
      </div>

      <ScoutDemo compact={true} />

      <div style={{
        display: 'flex', alignItems: 'center', gap: 8,
        fontSize: 11, color: 'var(--cal-muted)',
        fontFamily: AIPD_MONO, letterSpacing: '0.06em',
        paddingTop: 8, borderTop: '1px solid var(--cal-line-2)',
      }}>
        <span style={{ width: 7, height: 7, borderRadius: 999, background: '#16A34A' }} />
        1,025 matched · under 15 seconds · your call on who to advance.
      </div>
    </div>
  );
}

/* ══════════════════════════════════════════════════════════════
   TRIO — composite layout.
   Row 1: Scout demo full-width.
   Row 2: JD + CV side-by-side.
   ══════════════════════════════════════════════════════════════ */
function AIPDemos() {
  return (
    <div style={{ display: 'flex', flexDirection: 'column', gap: 20 }}>
      <ScoutDemoFrame />
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 20 }}>
        <JDDraftDemo />
        <CVParsingDemo />
      </div>
    </div>
  );
}

Object.assign(window, { AIPDemos, JDDraftDemo, CVParsingDemo, ScoutDemoFrame });
