// ReviewDeskPage — the owner's decision queue, live from the reception ledger.
// Three gates surfaced (nothing here EXECUTES anything — vault campaigns run in
// vault sessions with explicit owner go):
//   1. Scholar queue — 185 over-claim flags + 91 Latin discoveries
//   2. §V backfill  — the 601 studies authored before the reception template
//   3. OT Isaiah fleet — stopped per owner; resume needs explicit go
function ReviewDeskPage({ onOpenVaultStudy, onOpenSource }) {
  const [d, setD] = React.useState(null);
  const [err, setErr] = React.useState(null);
  const [showFlags, setShowFlags] = React.useState(false);

  React.useEffect(() => {
    let on = true;
    if (!window.AmosAPI || !window.AmosAPI.reviewdesk) { setErr('no-api'); return; }
    window.AmosAPI.reviewdesk()
      .then(x => { if (on) setD(x); })
      .catch(e => { if (on) setErr(String(e)); });
    return () => { on = false; };
  }, []);

  const TIERS = (d && d.ledger && d.ledger.tiers) || {};
  const tierChip = (t, label) => (TIERS[t] != null) && React.createElement('span', {
    key: t, className: 'rd-tier tl-chip ' + t, title: label
  }, label + ' ' + TIERS[t]);

  const flagRows = (d && d.flags && d.flags.rows) || [];
  const visibleFlags = showFlags ? flagRows : flagRows.slice(0, 25);

  return React.createElement('div', { className: 'amos-main rd-main' },
    React.createElement('section', { className: 'rd-hero' },
      React.createElement('div', { className: 'vsv-hero-eyebrow' }, 'THE AMOS PROJECT · OWNER'),
      React.createElement('h1', { className: 'rd-title' }, 'Review Desk'),
      React.createElement('p', { className: 'rd-sub' },
        'What awaits your judgment — live from the reception ledger. Display policy: everything ships visible at its current trust level; your review reclassifies, it never unhides.')
    ),

    err && React.createElement('div', { className: 'rd-err' },
      err === 'no-api'
        ? 'The Review Desk needs the local review API. Start it with amos-review-api/run.sh.'
        : 'Could not reach the review API: ' + err),

    d && React.createElement(React.Fragment, null,
      // ledger trust summary
      React.createElement('section', { className: 'rd-card' },
        React.createElement('div', { className: 'rd-card-head' }, 'Ledger trust profile',
          React.createElement('span', { className: 'rd-card-sub' },
            (d.ledger.records || 0).toLocaleString() + ' citation rows · generated ' + (d.ledger.generated || '—'))),
        React.createElement('div', { className: 'rd-tiers' },
          tierChip('ol', 'OL ✓'),
          tierChip('verified', 'VERIFIED'),
          tierChip('semantic', 'V-SEM'),
          tierChip('circular', '◐ CIRC'),
          tierChip('flagged', '⚠ REVIEW'),
          tierChip('contested', 'CONTESTED'),
          tierChip('synthesis', 'SYNTH'),
          tierChip('untagged', '—')
        )
      ),

      // Gate 1 — scholar queue
      React.createElement('section', { className: 'rd-card' },
        React.createElement('div', { className: 'rd-card-head' }, '1 · Scholar queue',
          React.createElement('span', { className: 'rd-card-sub' }, 'your judgment, prepared and packaged')),
        React.createElement('p', { className: 'rd-p' },
          React.createElement('b', null, d.flags.count + ' over-claim flags'),
          ' — VERIFIED tags whose original-language check suggests thematic use rather than quotation. Each stays visible with its ⚠ badge until you reclassify or defend it.'),
        React.createElement('div', { className: 'rd-flags' },
          visibleFlags.map((f, i) => React.createElement('div', { key: i, className: 'rd-flagrow' },
            React.createElement('span', { className: 'tl-chip flagged' }, '⚠'),
            React.createElement('span', { className: 'rd-flag-what' },
              (f.author_id || '?').replace(/-/g, ' ') + ' · ' + (f.work_slug || '').replace(/-/g, ' ') +
              (f.locator ? ' ' + f.locator : '') + '  —  ' + f.study),
            React.createElement('button', {
              className: 'rd-openlink',
              onClick: () => onOpenVaultStudy && onOpenVaultStudy(f.study)
            }, 'Open study →')
          )),
          flagRows.length > 25 && React.createElement('button', {
            className: 'tl-lowtoggle', onClick: () => setShowFlags(s => !s)
          }, showFlags ? 'Show fewer' : 'Show all ' + flagRows.length + ' flags')
        ),
        React.createElement('p', { className: 'rd-p', style: { marginTop: 18 } },
          React.createElement('b', null, d.discoveries.count + ' verbatim Latin discoveries'),
          ' — quotations the matcher found that the studies never claimed, grouped under 66 target studies. Insertion is the scholar’s call, never automatic.'),
        React.createElement('button', {
          className: 'rd-openlink big',
          onClick: () => onOpenSource && onOpenSource({
            path: d.discoveries.packet, label: 'Latin discovery insertion packet'
          })
        }, 'Open the insertion packet →')
      ),

      // Gate 2 — §V backfill
      React.createElement('section', { className: 'rd-card' },
        React.createElement('div', { className: 'rd-card-head' }, '2 · Reception backfill (§V)',
          React.createElement('span', { className: 'rd-card-sub' }, 'never commissioned — a new campaign to authorize')),
        React.createElement('p', { className: 'rd-p' },
          React.createElement('b', null, d.backfill.count + ' studies'),
          ' were authored before the reception template and carry no §V layer at all (their ledger is honestly empty — e.g. ROM 9:30–33). Distribution:'),
        React.createElement('div', { className: 'rd-tiers' },
          Object.entries(d.backfill.by_book).map(([b, n]) =>
            React.createElement('span', { key: b, className: 'rd-bookchip' }, b + ' ' + n))
        ),
        React.createElement('button', {
          className: 'rd-openlink big',
          onClick: () => onOpenSource && onOpenSource({
            path: d.backfill.plan, label: '§V backfill — campaign plan (DRAFT)'
          })
        }, 'Read the draft campaign plan →')
      ),

      // Gate 3 — OT fleet
      React.createElement('section', { className: 'rd-card' },
        React.createElement('div', { className: 'rd-card-head' }, '3 · OT reception — ' + d.ot.book,
          React.createElement('span', { className: 'rd-card-sub' }, 'vault campaign · not launchable from this app')),
        React.createElement('p', { className: 'rd-p' },
          React.createElement('b', null, d.ot.done + ' of ' + d.ot.total + ' pericopes done. '),
          d.ot.status + '. Resuming is a vault-session decision (~1.6M words for the full Isaiah build); this desk only reports it.'),
        React.createElement('button', {
          className: 'rd-openlink',
          onClick: () => onOpenSource && onOpenSource({
            path: d.ot.source, label: 'Session handoff — 2026-06-16'
          })
        }, 'Read the session handoff →')
      )
    )
  );
}

Object.assign(window, { ReviewDeskPage });
