// doc-provenance.jsx — "where did this number come from?" viewer.
// Renders the original PDF and highlights the source line of each parsed item,
// so extracted data can be double-checked against the document at a glance.
//
// No parser/prompt changes: the viewer re-reads the PDF text layer (which
// carries page + x/y for every glyph run), reconstructs lines with the same
// Y-grouping the parser uses, then locates each item by scoring lines on
// description tokens, SKU, and the qty/unit/amount numbers. Works on the
// in-memory file right after upload AND retroactively on stored invoices
// via their Firebase Storage _fileUrl.
//
// Pure logic (pdfSourceOf / extractDocLines / locateItemLines) lives in
// doc-provenance-core.js so Node tests can run it against real PDFs.

// ── shared document loader ──────────────────────────────────────────────────
// Loads a document's PDF, reconstructs its text-layer lines, and locates ALL
// items at once (locateAllItems — cross-item claiming prevents the highlight
// of one row from bleeding onto a sibling's description line). Cached at
// module scope per file so the trace modal and every row snippet share one
// parse per document for the whole page session.
const _docSourceCache = new Map();
function loadDocSources(doc) {
  const src = pdfSourceOf(doc);
  if (!src) return Promise.reject(new Error('No PDF available for this document.'));
  const key = (doc._fileUrl || doc._fileName || doc.id) + (src.file ? '#local' : '#remote');
  if (!_docSourceCache.has(key)) {
    const p = (async () => {
      const param = src.file
        ? { data: await src.file.arrayBuffer(), isEvalSupported: false } // CVE-2024-4367 mitigation
        : { url: src.url, isEvalSupported: false };
      const pdf      = await pdfjsLib.getDocument(param).promise;
      const docLines = await extractDocLines(pdf);
      const locs     = locateAllItems(doc.items || [], docLines);
      return { pdf, docLines, locs };
    })();
    p.catch(() => _docSourceCache.delete(key)); // don't cache failures
    _docSourceCache.set(key, p);
  }
  return _docSourceCache.get(key);
}

// A bare "Failed to fetch" means the stored PDF was loaded over the network
// (no in-session File) and the cross-origin request to Firebase Storage was
// blocked — usually a missing bucket CORS policy.
const _loadErrMsg = (e) =>
  /failed to fetch|load failed|networkerror/i.test(e?.message || '')
    ? "Couldn't fetch the stored PDF. Tracing reads the file you uploaded this session — re-upload it here to trace, or add a CORS policy to the Firebase Storage bucket so saved PDFs can be fetched."
    : (e?.message || 'Could not load the PDF.');

// ── modal viewer ────────────────────────────────────────────────────────────
function DocProvenanceModal({ invoice, onClose }) {
  const [pdf, setPdf]         = React.useState(null);
  const [docLines, setLines]  = React.useState(null);
  const [locs, setLocs]       = React.useState(null);
  const [err, setErr]         = React.useState(null);
  const [sel, setSel]         = React.useState(0);
  const [pageNum, setPageNum] = React.useState(1);
  const [vp, setVp]           = React.useState(null); // current page viewport (CSS px)
  const canvasRef  = React.useRef(null);
  const scrollRef  = React.useRef(null);
  const renderTask = React.useRef(null);

  // Load document + reconstruct lines + locate items once (shared cache).
  React.useEffect(() => {
    let dead = false;
    loadDocSources(invoice)
      .then(({ pdf, docLines, locs }) => {
        if (dead) return;
        setPdf(pdf); setLines(docLines); setLocs(locs);
      })
      .catch((e) => { if (!dead) setErr(_loadErrMsg(e)); });
    return () => { dead = true; };
  }, [invoice]);

  const selLines = (locs && locs[sel] ? locs[sel] : []).map((i) => docLines[i]);

  // Jump to the selected item's page.
  React.useEffect(() => {
    if (selLines.length) setPageNum(selLines[0].page);
  }, [sel, locs]);

  // Render the current page to canvas (devicePixelRatio-aware).
  React.useEffect(() => {
    if (!pdf || !canvasRef.current) return;
    let dead = false;
    (async () => {
      try {
        const page  = await pdf.getPage(pageNum);
        const cw    = (scrollRef.current?.clientWidth || 820) - 24;
        const scale = Math.min(2, cw / page.getViewport({ scale: 1 }).width);
        const view  = page.getViewport({ scale });
        const dpr   = window.devicePixelRatio || 1;
        const canvas = canvasRef.current;
        if (!canvas || dead) return;
        canvas.width  = Math.floor(view.width * dpr);
        canvas.height = Math.floor(view.height * dpr);
        canvas.style.width  = `${view.width}px`;
        canvas.style.height = `${view.height}px`;
        renderTask.current?.cancel?.();
        renderTask.current = page.render({
          canvasContext: canvas.getContext('2d'),
          viewport: view,
          transform: dpr !== 1 ? [dpr, 0, 0, dpr, 0, 0] : null,
        });
        await renderTask.current.promise;
        if (!dead) setVp(view);
      } catch (e) {
        if (e?.name !== 'RenderingCancelledException' && !dead) setErr(e.message);
      }
    })();
    return () => { dead = true; };
  }, [pdf, pageNum]);

  // Highlight rectangles for the selected item on the current page (CSS px).
  const rects = React.useMemo(() => {
    if (!vp) return [];
    return selLines
      .filter((ln) => ln.page === pageNum)
      .map((ln) => {
        const r = vp.convertToViewportRectangle([ln.x0, ln.y0, ln.x1, ln.y1]);
        return {
          left: Math.min(r[0], r[2]), top: Math.min(r[1], r[3]),
          width: Math.abs(r[2] - r[0]), height: Math.abs(r[3] - r[1]),
        };
      });
  }, [vp, sel, locs, pageNum]);

  // Bring the first highlight into view after each render.
  React.useEffect(() => {
    if (rects.length && scrollRef.current) {
      scrollRef.current.scrollTo({ top: Math.max(0, rects[0].top - 120), behavior: 'smooth' });
    }
  }, [rects]);

  React.useEffect(() => {
    const onKey = (e) => { if (e.key === 'Escape') onClose(); };
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, [onClose]);

  const numPages = pdf?.numPages || 1;
  const items    = invoice.items || [];

  return (
    <div className="dpv-backdrop" onClick={(e) => { if (e.target === e.currentTarget) onClose(); }}>
      <div className="dpv" role="dialog" aria-modal="true" aria-label="Document source viewer">
        <div className="dpv-hd">
          <div className="dpv-title">
            <span className="mono">{invoice._fileName || invoice.id}</span>
            <span className="dpv-sub">click a line item to see where it was read from</span>
          </div>
          <div className="dpv-nav">
            <button className="btn btn-ghost btn-sm" disabled={pageNum <= 1}
              onClick={() => setPageNum((p) => p - 1)} aria-label="Previous page">‹</button>
            <span className="dpv-page num">{pageNum} / {numPages}</span>
            <button className="btn btn-ghost btn-sm" disabled={pageNum >= numPages}
              onClick={() => setPageNum((p) => p + 1)} aria-label="Next page">›</button>
            <button className="btn btn-ghost btn-sm" onClick={onClose} aria-label="Close viewer">
              <Icon name="x" size={14} />
            </button>
          </div>
        </div>

        <div className="dpv-body">
          <div className="dpv-items">
            {items.map((it, i) => {
              const located = locs ? (locs[i] || []).length > 0 : null;
              return (
                <button key={i}
                  className={`dpv-item ${i === sel ? 'is-sel' : ''}`}
                  onClick={() => setSel(i)}>
                  <span className="dpv-item-desc" title={it.desc}>{it.desc}</span>
                  <span className="dpv-item-meta">
                    {it.sku && <span className="mono dpv-item-sku">{it.sku}</span>}
                    <span className="num">{(it.qty ?? 0).toLocaleString()}{it.uom ? ' ' + it.uom : ''} @ {it.unit != null ? fmtUSD(it.unit) : '—'}</span>
                    {located === false && <span className="dpv-notfound">not located</span>}
                  </span>
                </button>
              );
            })}
            {items.length === 0 && <div className="dpv-empty">No line items.</div>}
          </div>

          <div className="dpv-doc" ref={scrollRef}>
            {err ? (
              <div className="dpv-err">{err}</div>
            ) : !pdf ? (
              <div className="dpv-loading"><div className="dpv-spin" /> Loading document…</div>
            ) : (
              <div className="dpv-canvas-wrap">
                <canvas ref={canvasRef} />
                {rects.map((r, k) => (
                  <div key={k} className="dpv-hl" style={{
                    left: r.left, top: r.top, width: r.width, height: r.height,
                  }} />
                ))}
                {locs && locs[sel] && locs[sel].length === 0 && (
                  <div className="dpv-banner">
                    Couldn&rsquo;t locate this item in the text layer — it may be wrapped
                    across lines or the PDF may be a scan (no embedded text).
                  </div>
                )}
              </div>
            )}
          </div>
        </div>

        <DocProvenanceStyles />
      </div>
    </div>
  );
}

const DocProvenanceStyles = () => (
  <style>{`
    .dpv-backdrop {
      position: fixed; inset: 0; z-index: 400;
      background: oklch(0 0 0 / 0.55);
      display: grid; place-items: center; padding: 24px;
    }
    .dpv {
      width: min(1280px, 96vw); height: min(860px, 92vh);
      background: var(--surface); border: 1px solid var(--border-strong);
      border-radius: var(--r-lg); box-shadow: var(--shadow-2);
      display: flex; flex-direction: column; overflow: hidden;
    }
    .dpv-hd {
      display: flex; align-items: center; justify-content: space-between; gap: 12px;
      padding: 10px 14px; border-bottom: 1px solid var(--border); flex-shrink: 0;
    }
    .dpv-title { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
    .dpv-title .mono { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .dpv-sub { font-size: 11.5px; color: var(--fg-dim); white-space: nowrap; }
    .dpv-nav { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
    .dpv-page { font-size: 12px; color: var(--fg-muted); padding: 0 4px; }

    .dpv-body { flex: 1; display: grid; grid-template-columns: minmax(260px, 340px) 1fr; min-height: 0; }
    .dpv-items { overflow-y: auto; border-right: 1px solid var(--border); padding: 8px; display: flex; flex-direction: column; gap: 4px; }
    .dpv-item {
      appearance: none; text-align: left; font: inherit; cursor: pointer;
      background: var(--surface-2); color: var(--fg);
      border: 1px solid var(--border); border-radius: var(--r);
      padding: 8px 10px; display: flex; flex-direction: column; gap: 3px;
    }
    .dpv-item:hover { border-color: var(--border-strong); }
    .dpv-item.is-sel { border-color: var(--accent); background: oklch(from var(--accent) l c h / 0.10); }
    .dpv-item-desc { font-size: 12px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .dpv-item-meta { display: flex; align-items: center; gap: 8px; font-size: 10.5px; color: var(--fg-dim); }
    .dpv-item-sku { background: var(--surface-3); padding: 0 5px; border-radius: 3px; }
    .dpv-notfound { color: var(--warn); font-weight: 500; }
    .dpv-empty { padding: 20px; color: var(--fg-dim); font-size: 12px; text-align: center; }

    .dpv-doc { overflow: auto; background: var(--surface-2); padding: 12px; position: relative; }
    .dpv-canvas-wrap { position: relative; width: fit-content; margin: 0 auto; box-shadow: var(--shadow-2); }
    .dpv-canvas-wrap canvas { display: block; }
    .dpv-hl {
      position: absolute; pointer-events: none;
      background: oklch(0.85 0.17 95 / 0.45);     /* highlighter yellow */
      outline: 2px solid oklch(0.75 0.15 75 / 0.9);
      border-radius: 2px; mix-blend-mode: multiply;
    }
    .dpv-banner {
      position: sticky; bottom: 8px; left: 0; right: 0; margin: 8px;
      padding: 8px 12px; border-radius: var(--r); font-size: 12px;
      background: var(--warn-bg); color: var(--warn);
      border: 1px solid oklch(from var(--warn) l calc(c*.5) h / .4);
    }
    .dpv-err { padding: 32px; color: var(--bad); font-size: 13px; text-align: center; }
    .dpv-loading { display: flex; align-items: center; justify-content: center; gap: 10px; height: 100%; color: var(--fg-dim); font-size: 13px; }
    .dpv-spin { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--border); border-top-color: var(--accent); animation: dz-spin .7s linear infinite; }

    @media (max-width: 760px) { .dpv-body { grid-template-columns: 1fr; } .dpv-items { display: none; } }
  `}</style>
);

// ── row source snippets ─────────────────────────────────────────────────────
// Inline "where did this number come from?" panes for a budget row: a cropped
// strip of the original PDF around the located line, with the highlight, for
// the quote AND each invoice the row drew from. Falls back to the parsed
// values when a document has no PDF (pasted text / spreadsheet) or the line
// can't be located in the text layer.

// Items inside budget rows are matcher clones — recover their index in the
// source document so the cached whole-document locations can be looked up.
function _itemIndexIn(doc, item) {
  if (typeof item._qIndex === 'number') return item._qIndex;
  const m = /::(\d+)$/.exec(item._id || '');
  if (m) return +m[1];
  return (doc.items || []).findIndex((it) =>
    it.desc === item.desc && it.qty === item.qty && (it.sku || '') === (item.sku || ''));
}

function PdfSnippet({ kind, doc, item }) {
  const [state, setState] = React.useState({ status: 'loading' });
  const [hl, setHl]       = React.useState([]);   // highlight rects within the crop
  const [pageNo, setPage] = React.useState(null);
  const wrapRef   = React.useRef(null);
  const canvasRef = React.useRef(null);

  React.useEffect(() => {
    let dead = false;
    (async () => {
      try {
        if (!pdfSourceOf(doc)) { setState({ status: 'nopdf' }); return; }
        const { pdf, docLines, locs } = await loadDocSources(doc);
        if (dead) return;
        const idx      = _itemIndexIn(doc, item);
        const lineIdxs = (idx >= 0 && locs[idx]) || [];
        if (!lineIdxs.length) { setState({ status: 'notfound' }); return; }

        const lines   = lineIdxs.map((i) => docLines[i]);
        const pageNum = lines[0].page;
        const page    = await pdf.getPage(pageNum);
        if (dead) return;
        // Full page width (rows span it), vertically cropped to the located
        // lines plus ~one neighboring row of context on each side.
        const cw    = wrapRef.current?.clientWidth || 560;
        const scale = cw / page.getViewport({ scale: 1 }).width;
        const view  = page.getViewport({ scale });
        const rects = lines.filter((ln) => ln.page === pageNum).map((ln) => {
          const r = view.convertToViewportRectangle([ln.x0, ln.y0, ln.x1, ln.y1]);
          return {
            left: Math.min(r[0], r[2]), top: Math.min(r[1], r[3]),
            width: Math.abs(r[2] - r[0]), height: Math.abs(r[3] - r[1]),
          };
        });
        const pad    = 22;
        const top    = Math.max(0, Math.min(...rects.map((r) => r.top)) - pad);
        const bottom = Math.min(view.height, Math.max(...rects.map((r) => r.top + r.height)) + pad);
        const dpr    = window.devicePixelRatio || 1;
        const canvas = canvasRef.current;
        if (!canvas) return;
        canvas.width  = Math.floor(view.width * dpr);
        canvas.height = Math.floor((bottom - top) * dpr);
        canvas.style.width  = `${view.width}px`;
        canvas.style.height = `${bottom - top}px`;
        await page.render({
          canvasContext: canvas.getContext('2d'),
          viewport: view,
          transform: [dpr, 0, 0, dpr, 0, -top * dpr],
        }).promise;
        if (dead) return;
        setHl(rects.map((r) => ({ ...r, top: r.top - top })));
        setPage(pageNum);
        setState({ status: 'ok' });
      } catch (e) {
        if (e?.name !== 'RenderingCancelledException' && !dead) {
          setState({ status: 'err', msg: _loadErrMsg(e) });
        }
      }
    })();
    return () => { dead = true; };
  }, [doc, item]);

  const { status } = state;
  const parsedLine = [
    item.qty != null ? `${item.qty.toLocaleString()}${item.uom ? ' ' + item.uom : ''}` : null,
    item.unit != null ? `@ ${fmtUSD(item.unit)}` : null,
    item.desc,
  ].filter(Boolean).join(' · ');

  return (
    <div className="rsnip">
      <div className="rsnip-hd">
        <span className={`rsnip-tag rsnip-tag-${kind}`}>{kind === 'quote' ? 'Quote' : 'Invoice'}</span>
        <span className="rsnip-name mono" title={doc._fileName || doc.id}>{doc._fileName || doc.id}</span>
        {status === 'ok' && pageNo != null && <span className="rsnip-page">p. {pageNo}</span>}
      </div>
      <div className="rsnip-body" ref={wrapRef}>
        <div className="rsnip-canvas-wrap" style={{ display: status === 'ok' ? 'block' : 'none' }}>
          <canvas ref={canvasRef} />
          {hl.map((r, k) => (
            <div key={k} className="rsnip-hl" style={{
              left: r.left, top: r.top, width: r.width, height: r.height,
            }} />
          ))}
        </div>
        {status === 'loading' && <div className="rsnip-wait"><span className="rsnip-spin" /> Loading source…</div>}
        {status !== 'ok' && status !== 'loading' && (
          <div className="rsnip-fallback">
            <div className="rsnip-fb-line">{parsedLine}</div>
            <div className="rsnip-fb-note">
              {status === 'nopdf'    && 'No PDF for this document — values above are as parsed.'}
              {status === 'notfound' && 'Couldn’t locate this line on the PDF (it may be wrapped oddly or a scan).'}
              {status === 'err'      && state.msg}
            </div>
          </div>
        )}
      </div>
    </div>
  );
}

// One pane per source document the row touches: the quote line (when the row
// is on the quote) and each invoice line it drew from. Rows that exist on
// only one side naturally show just that side.
function RowSourcePanel({ row, quote, invoices }) {
  // attachLocalFile clones the doc, so memoize — otherwise every parent
  // re-render changes doc identity and the snippets reload for nothing.
  const srcs = React.useMemo(() => {
    const out = [];
    if (row.quoteItem && quote) {
      out.push({ key: 'q', kind: 'quote', doc: attachLocalFile(quote), item: row.quoteItem });
    }
    (row.breakdown || []).forEach((b, i) => {
      if (!b.srcItem) return;
      const inv = (invoices || []).find((iv) => iv.id === b.invoiceId);
      if (inv) out.push({ key: `i${i}`, kind: 'invoice', doc: attachLocalFile(inv), item: b.srcItem });
    });
    return out;
  }, [row, quote, invoices]);
  if (srcs.length === 0) return null;

  return (
    <div className={`rsp ${srcs.length > 1 ? 'rsp-multi' : ''}`}>
      {srcs.map((s) => <PdfSnippet key={s.key} kind={s.kind} doc={s.doc} item={s.item} />)}
      <RowSourceStyles />
    </div>
  );
}

const RowSourceStyles = () => (
  <style>{`
    .rsp { display: grid; grid-template-columns: 1fr; gap: 8px; padding-top: 8px; }
    .rsp-multi { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
    .rsnip {
      background: var(--surface); border: 1px solid var(--border);
      border-radius: var(--r); overflow: hidden; min-width: 0;
    }
    .rsnip-hd {
      display: flex; align-items: center; gap: 7px;
      padding: 5px 8px; border-bottom: 1px solid var(--border);
      background: var(--surface-2);
    }
    .rsnip-tag {
      font-size: 9px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.07em;
      padding: 1px 6px; border-radius: 3px; flex-shrink: 0;
    }
    .rsnip-tag-quote   { background: var(--surface-3); color: var(--fg-muted); border: 1px solid var(--border); }
    .rsnip-tag-invoice { background: oklch(from var(--info) l c h / 0.12); color: var(--info); border: 1px solid oklch(from var(--info) l calc(c*.4) h / .3); }
    .rsnip-name { font-size: 10.5px; color: var(--fg-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
    .rsnip-page { font-size: 10px; color: var(--fg-dim); flex-shrink: 0; }
    .rsnip-body { background: white; }
    .rsnip-canvas-wrap { position: relative; }
    .rsnip-canvas-wrap canvas { display: block; }
    .rsnip-hl {
      position: absolute; pointer-events: none;
      background: oklch(0.85 0.17 95 / 0.45);     /* highlighter yellow */
      outline: 2px solid oklch(0.75 0.15 75 / 0.9);
      border-radius: 2px; mix-blend-mode: multiply;
    }
    .rsnip-wait {
      display: flex; align-items: center; gap: 8px; padding: 14px 10px;
      font-size: 11px; color: var(--fg-dim); background: var(--surface);
    }
    .rsnip-spin {
      width: 14px; height: 14px; border-radius: 50%; flex-shrink: 0;
      border: 2px solid var(--border); border-top-color: var(--accent);
      animation: dz-spin .7s linear infinite;
    }
    .rsnip-fallback { padding: 9px 10px; display: flex; flex-direction: column; gap: 4px; background: var(--surface); }
    .rsnip-fb-line { font-size: 11.5px; color: var(--fg); }
    .rsnip-fb-note { font-size: 10.5px; color: var(--fg-dim); line-height: 1.45; }
  `}</style>
);

Object.assign(window, { DocProvenanceModal, RowSourcePanel, loadDocSources });
