// invodiff-drawdown.jsx — Material Drawdown: per-item Ordered → Received →
// Remaining against the RFQ baseline. Answers the PM's literal question — "I had
// 100 90s, 50 came in, 50 left" — and surfaces over-pulls as a negative remaining.
// Quantity-first companion to the dollar-oriented Budget and Variance views; reads
// the exact same aggregateInvoices output so the three screens never disagree.

// Remaining → tone band. `na` = units couldn't be reconciled (UOM mismatch).
function ddTone(remaining, received) {
  if (remaining == null) return 'na';
  if (remaining < 0) return 'over';                 // pulled more than quoted
  if (remaining === 0 && received > 0) return 'done';
  if (received > 0) return 'open';                  // partially received
  return 'idle';                                    // nothing invoiced yet
}

function DrawdownRow({ row, expanded, onToggle }) {
  const ordered    = row.quotedQty;
  const received   = row.invoicedQty || 0;
  const mismatch   = row.flags.uom;
  const comparable = !mismatch && ordered != null;
  const remaining  = comparable ? +(ordered - received).toFixed(2) : null;
  const tone       = ddTone(remaining, received);
  const pct        = ordered > 0 ? Math.min(received / ordered, 1) : (received > 0 ? 1 : 0);
  const over       = comparable && remaining < 0;
  const uom        = row.uomNorm || '';
  const priceUp    = row.quotedUnit != null && row.invoicedAvgUnit != null && row.invoicedAvgUnit > row.quotedUnit + 0.005;
  const unitPct    = (row.quotedUnit > 0 && row.invoicedAvgUnit != null)
    ? ((row.invoicedAvgUnit - row.quotedUnit) / row.quotedUnit) * 100 : null;
  const hasBreakdown = (row.breakdown || []).length > 0;

  return (
    <>
      <div className={`ddr ddr-${tone}`} role="row">
        <div className="ddr-c ddr-item" role="cell">
          <button
            className={`ddr-exp${hasBreakdown ? '' : ' is-empty'}`}
            onClick={hasBreakdown ? onToggle : undefined}
            disabled={!hasBreakdown}
            aria-label={expanded ? 'Collapse invoice detail' : 'Show invoice detail'}
          >
            <Icon name="chev" size={12} style={{ transform: expanded ? 'rotate(90deg)' : 'none', transition: 'transform .12s' }} />
          </button>
          <div className="ddr-item-main">
            <span className="ddr-desc" title={row.desc}>{row.desc}</span>
            {row.sku && row.sku !== '—' && <span className="ddr-sku mono">{row.sku}</span>}
          </div>
        </div>

        <div className="ddr-c ddr-num num" role="cell">
          {ordered != null ? ordered.toLocaleString() : '—'}{uom && <span className="ddr-uom"> {uom}</span>}
        </div>

        <div className={`ddr-c ddr-num num${over ? ' ddr-over-txt' : ''}`} role="cell">
          {received.toLocaleString()}{mismatch && row.invoicedUOM && <span className="ddr-uom"> {row.invoicedUOM}</span>}
        </div>

        <div className="ddr-c ddr-remaining num" role="cell">
          {comparable
            ? <span className={`ddr-rem ddr-rem-${tone}`}>{remaining.toLocaleString()}</span>
            : <span className="ddr-mismatch">unit mismatch</span>}
        </div>

        <div className="ddr-c ddr-prog" role="cell">
          <div className="ddr-bar"><div className={`ddr-bar-fill ddr-bar-${tone}`} style={{ width: `${pct * 100}%` }} /></div>
          {over ? <span className="ddr-prog-tag ddr-prog-over">over</span>
            : tone === 'done' ? <span className="ddr-prog-tag ddr-prog-done">full</span>
            : <span className="ddr-prog-pct num">{Math.round(pct * 100)}%</span>}
        </div>

        <div className="ddr-c ddr-num num" role="cell">{row.quotedUnit != null ? fmtUSD(row.quotedUnit) : '—'}</div>

        <div className={`ddr-c ddr-num num${priceUp ? ' ddr-priceup' : ''}`} role="cell">
          {row.invoicedAvgUnit != null ? fmtUSD(row.invoicedAvgUnit) : '—'}
          {unitPct != null && Math.abs(unitPct) >= 0.5 && (
            <span className="ddr-unit-delta">{unitPct >= 0 ? '+' : ''}{unitPct.toFixed(0)}%</span>
          )}
        </div>
      </div>

      {expanded && hasBreakdown && (
        <div className="ddr-detail" role="row">
          <div className="ddr-detail-inner">
            <div className="ddr-detail-hd">Invoice history</div>
            {row.breakdown.map((b, i) => (
              <div key={i} className="ddr-bd">
                <span className="ddr-bd-id mono">{b.invoiceId}</span>
                <span className="ddr-bd-date">{b.date || '—'}</span>
                <span className="ddr-bd-qty num">{(b.srcQty ?? b.qty).toLocaleString()} {b.srcUom || row.uomNorm || ''}</span>
                <span className="ddr-bd-at">@ {fmtUSD(b.unit)}</span>
                <span className="ddr-bd-tot num">{fmtUSD(b.lineTotal)}</span>
              </div>
            ))}
          </div>
        </div>
      )}
    </>
  );
}

function DrawdownKpi({ label, value, sub, tone = 'default' }) {
  return (
    <div className={`ddk ddk-${tone}`}>
      <div className="ddk-label">{label}</div>
      <div className="ddk-value num">{value}</div>
      {sub && <div className="ddk-sub">{sub}</div>}
    </div>
  );
}

function DrawdownView({ job, userId, vendorRules }) {
  const [quote, setQuote]       = React.useState(null);
  const [invoices, setInvoices] = React.useState([]);
  const [synced, setSynced]     = React.useState(false);
  const [filter, setFilter]     = React.useState('all'); // all | open | over | done
  const [expanded, setExpanded] = React.useState(() => new Set());

  const isSample = job.id === '_sample';
  const jobRef = React.useMemo(
    () => !isSample ? fbDb.collection('jobs').doc(job.id) : null,
    [job.id]
  );

  React.useEffect(() => {
    setExpanded(new Set());
    if (isSample) {
      setQuote(INVOICE_A); setInvoices([INVOICE_B]); setSynced(true);
      return;
    }
    setSynced(false);
    const unsub = jobRef.onSnapshot(
      (doc) => {
        if (doc.exists) {
          const d = doc.data();
          setQuote(d.quote ?? null);
          setInvoices((d.invoices ?? []).filter((inv) => !inv._hidden));
        }
        setSynced(true);
      },
      () => setSynced(true)
    );
    return unsub;
  }, [job.id]);

  const rules = React.useMemo(
    () => (vendorRules || {})[quote?.vendor || ''] || [],
    [vendorRules, quote?.vendor]
  );

  const result = React.useMemo(
    () => quote && invoices.length > 0 ? aggregateInvoices(quote, invoices, rules) : null,
    [quote, invoices, rules]
  );

  // Quote-backed material lines (exclude synthetic shipping/tax + off-quote adds).
  const items    = React.useMemo(() => result ? result.rows.filter((r) => !r.synthetic && !r.flags.added) : [], [result]);
  const offQuote = React.useMemo(() => result ? result.rows.filter((r) => !r.synthetic && r.flags.added) : [], [result]);

  const counts = React.useMemo(() => {
    const a = { over: 0, done: 0, open: 0, idle: 0, na: 0 };
    for (const r of items) {
      const ordered = r.quotedQty, received = r.invoicedQty || 0;
      const comparable = !r.flags.uom && ordered != null;
      a[ddTone(comparable ? ordered - received : null, received)]++;
    }
    return a;
  }, [items]);

  const shown = React.useMemo(() => items.filter((r) => {
    if (filter === 'all') return true;
    const ordered = r.quotedQty, received = r.invoicedQty || 0;
    const comparable = !r.flags.uom && ordered != null;
    const t = ddTone(comparable ? ordered - received : null, received);
    if (filter === 'open') return t === 'open' || t === 'idle';
    if (filter === 'over') return t === 'over';
    if (filter === 'done') return t === 'done';
    return true;
  }), [items, filter]);

  const toggle = (key) => setExpanded((prev) => {
    const next = new Set(prev);
    next.has(key) ? next.delete(key) : next.add(key);
    return next;
  });

  if (!synced) {
    return <div className="dd-wrap"><div className="dd-loading"><div className="dd-spin" /></div><DrawdownStyles /></div>;
  }
  if (!quote) {
    return (
      <div className="dd-wrap">
        <div className="dd-state">
          <div className="dd-state-icon"><Icon name="layers" size={28} /></div>
          <div className="dd-state-h">No quote loaded</div>
          <div className="dd-state-sub">Upload an RFQ/quote in the Budget tab — material drawdown tracks every quoted item from there.</div>
        </div>
        <DrawdownStyles />
      </div>
    );
  }
  if (invoices.length === 0) {
    return (
      <div className="dd-wrap">
        <div className="dd-state">
          <div className="dd-state-icon"><Icon name="file" size={28} /></div>
          <div className="dd-state-h">No invoices yet</div>
          <div className="dd-state-sub">Add invoices in the Budget tab — each one draws down the quoted quantities here.</div>
        </div>
        <DrawdownStyles />
      </div>
    );
  }

  const remainingBudget = +(result.summary.quotedTotal - result.summary.invoicedTotal).toFixed(2);
  const FILTERS = [
    ['all',  'All items',  items.length],
    ['open', 'Outstanding', counts.open + counts.idle],
    ['over', 'Over-pulled', counts.over],
    ['done', 'Complete',    counts.done],
  ];

  return (
    <div className="dd-wrap">
      <div className="dd-hero">
        <div className="dd-hero-tag mono">JOB {job.jobNumber} · MATERIALS</div>
        <h1 className="dd-hero-h">Material Drawdown</h1>
        <p className="dd-hero-sub">
          How much of each quoted item has been invoiced, and what's left to order. A negative
          <span className="dd-neg-chip">remaining</span> means more was billed than quoted.
        </p>
      </div>

      <div className="dd-kpis">
        <DrawdownKpi label="Quoted" value={fmtUSD(result.summary.quotedTotal)} sub={`${items.length} line item${items.length !== 1 ? 's' : ''}`} />
        <DrawdownKpi label="Invoiced" value={fmtUSD(result.summary.invoicedTotal)} sub={`${invoices.length} invoice${invoices.length !== 1 ? 's' : ''}`} tone="info" />
        <DrawdownKpi label="Remaining budget" value={fmtUSD(remainingBudget)} sub={remainingBudget < 0 ? 'over budget' : 'left to spend'} tone={remainingBudget < 0 ? 'bad' : 'ok'} />
        <DrawdownKpi label="Over-pulled" value={counts.over} sub={counts.over === 0 ? 'none negative' : 'items billed past quote'} tone={counts.over > 0 ? 'bad' : 'default'} />
      </div>

      <div className="dd-filters">
        {FILTERS.map(([key, label, count]) => (
          <button key={key} className={`dd-filter${filter === key ? ' is-on' : ''}`} onClick={() => setFilter(key)}>
            {label}<span className="dd-filter-count num">{count}</span>
          </button>
        ))}
      </div>

      <div className="dd-table" role="table" aria-label="Material drawdown">
        <div className="dd-head" role="row">
          <div className="dd-h dd-h-item" role="columnheader">Item</div>
          <div className="dd-h dd-h-num" role="columnheader">Ordered</div>
          <div className="dd-h dd-h-num" role="columnheader">Received</div>
          <div className="dd-h dd-h-num" role="columnheader">Remaining</div>
          <div className="dd-h dd-h-prog" role="columnheader">Progress</div>
          <div className="dd-h dd-h-num" role="columnheader">Quoted $/u</div>
          <div className="dd-h dd-h-num" role="columnheader">Charged $/u</div>
        </div>
        <div className="dd-body" role="rowgroup">
          {shown.map((r) => {
            const key = r.sku && r.sku !== '—' ? r.sku + '|' + r.desc : r.desc;
            return <DrawdownRow key={key} row={r} expanded={expanded.has(key)} onToggle={() => toggle(key)} />;
          })}
          {shown.length === 0 && <div className="dd-empty">No items match this filter.</div>}
        </div>
      </div>

      {offQuote.length > 0 && (
        <div className="dd-offquote">
          <div className="dd-offquote-hd">
            <Icon name="flag" size={13} />
            <span>Off-quote charges</span>
            <span className="dd-offquote-count num">{offQuote.length}</span>
            <span className="dd-offquote-note">Billed but not on the RFQ — nothing to draw down from.</span>
          </div>
          {offQuote.map((r, i) => (
            <div key={i} className="dd-oq-row">
              <span className="dd-oq-desc" title={r.desc}>{r.desc}</span>
              {r.sku && r.sku !== '—' && <span className="ddr-sku mono">{r.sku}</span>}
              <span className="dd-oq-qty num">{(r.invoicedQty || 0).toLocaleString()} {r.invoicedUOM || r.uomNorm || ''}</span>
              <span className="dd-oq-amt num">{fmtUSD(r.invoicedTotal)}</span>
            </div>
          ))}
        </div>
      )}

      <DrawdownStyles />
    </div>
  );
}

const DrawdownStyles = () => (
  <style>{`
    .dd-wrap { max-width: 1400px; margin: 0 auto; padding: 40px 24px 64px; display: flex; flex-direction: column; gap: 18px; }

    .dd-loading { display: flex; align-items: center; justify-content: center; min-height: 50vh; }
    .dd-spin { width: 28px; height: 28px; border-radius: 50%; border: 2.5px solid var(--border); border-top-color: var(--accent); animation: dz-spin .7s linear infinite; }
    .dd-state { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; min-height: 40vh; text-align: center; }
    .dd-state-icon { color: var(--fg-dim); opacity: 0.4; }
    .dd-state-h { font-size: 15px; font-weight: 600; color: var(--fg-muted); }
    .dd-state-sub { font-size: 13px; color: var(--fg-dim); max-width: 360px; line-height: 1.55; }
    .dd-empty { padding: 32px; text-align: center; color: var(--fg-dim); font-size: 13px; }

    /* ── Hero ────────────────────────────────────────────────────── */
    .dd-hero-tag { font-size: 11px; font-weight: 600; letter-spacing: 0.1em; text-transform: uppercase; color: var(--accent); margin-bottom: 6px; }
    .dd-hero-h   { font-size: 30px; font-weight: 600; letter-spacing: -0.025em; margin: 0 0 8px; }
    .dd-hero-sub { font-size: 14.5px; color: var(--fg-muted); margin: 0; line-height: 1.6; max-width: 640px; }
    .dd-neg-chip { font-family: "IBM Plex Mono", monospace; font-size: 11.5px; color: var(--bad); background: var(--bad-bg);
      border: 1px solid oklch(from var(--bad) l calc(c*.5) h / .35); border-radius: 4px; padding: 0 6px; margin: 0 4px; }

    /* ── KPI cards ───────────────────────────────────────────────── */
    .dd-kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
    .ddk { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); padding: 14px 16px; box-shadow: var(--shadow-1); border-top: 2px solid var(--border-strong); }
    .ddk-info { border-top-color: var(--accent); }
    .ddk-ok   { border-top-color: var(--ok); }
    .ddk-bad  { border-top-color: var(--bad); }
    .ddk-label { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.07em; color: var(--fg-dim); font-weight: 600; }
    .ddk-value { font-size: 24px; font-weight: 600; letter-spacing: -0.02em; margin-top: 4px; }
    .ddk-bad .ddk-value { color: var(--bad); }
    .ddk-sub { font-size: 11.5px; color: var(--fg-dim); margin-top: 2px; }

    /* ── Filters ─────────────────────────────────────────────────── */
    .dd-filters { display: flex; gap: 6px; flex-wrap: wrap; }
    .dd-filter { appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--fg-muted);
      padding: 5px 12px; border-radius: 999px; font: inherit; font-size: 12px; cursor: pointer;
      display: inline-flex; align-items: center; gap: 6px; transition: color .12s, background .12s, border-color .12s; }
    .dd-filter:hover:not(.is-on) { color: var(--fg); }
    .dd-filter.is-on { background: oklch(from var(--accent) l c h / 0.14); color: var(--accent); border-color: oklch(from var(--accent) l calc(c*.5) h / .4); font-weight: 600; }
    .dd-filter-count { font-size: 10.5px; background: var(--surface-3); color: var(--fg-dim); padding: 1px 6px; border-radius: 999px; }
    .dd-filter.is-on .dd-filter-count { background: oklch(from var(--accent) l c h / 0.22); color: var(--accent); }

    /* ── Table ───────────────────────────────────────────────────── */
    .dd-table { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); overflow: hidden; box-shadow: var(--shadow-1); }
    .dd-head, .ddr { display: grid; grid-template-columns: minmax(200px,1fr) 84px 84px 96px 132px 92px 104px; }
    .dd-head { background: var(--surface-2); border-bottom: 1px solid var(--border); }
    .dd-h { font-size: 10px; letter-spacing: 0.07em; text-transform: uppercase; color: var(--fg-dim); font-weight: 600; padding: 9px 12px; border-right: 1px solid var(--border); }
    .dd-h:last-child { border-right: none; }
    .dd-h-num { text-align: right; }
    .dd-h-prog { text-align: left; }

    .ddr { border-bottom: 1px solid var(--border); min-height: 40px; position: relative; }
    .ddr:last-of-type { border-bottom: none; }
    .ddr-over::before { content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 2px; background: var(--bad); }
    .ddr-c { padding: 8px 12px; font-size: 12px; color: var(--fg); border-right: 1px solid var(--border); display: flex; align-items: center; min-height: 40px; }
    .ddr-c:last-child { border-right: none; }
    .ddr-num { justify-content: flex-end; font-variant-numeric: tabular-nums; font-family: "IBM Plex Mono", monospace; }
    .ddr-item { gap: 6px; min-width: 0; }
    .ddr-exp { appearance: none; border: 0; background: transparent; color: var(--fg-dim); cursor: pointer; padding: 2px; display: grid; place-items: center; border-radius: 3px; flex-shrink: 0; }
    .ddr-exp:hover:not(.is-empty) { color: var(--fg); background: var(--surface-3); }
    .ddr-exp.is-empty { opacity: 0; cursor: default; }
    .ddr-item-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
    .ddr-desc { font-size: 12.5px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .ddr-sku { font-size: 10px; color: var(--fg-dim); background: var(--surface-3); border: 1px solid var(--border); padding: 0 5px; border-radius: 3px; width: fit-content; }
    .ddr-uom { color: var(--fg-dim); font-size: 10px; }
    .ddr-over-txt { color: var(--bad); font-weight: 600; }

    .ddr-remaining { justify-content: flex-end; font-family: "IBM Plex Mono", monospace; }
    .ddr-rem { font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; }
    .ddr-rem-open { color: var(--fg); }
    .ddr-rem-idle { color: var(--fg-muted); }
    .ddr-rem-done { color: var(--ok); }
    .ddr-rem-over { color: var(--bad); }
    .ddr-mismatch { font-size: 10px; color: var(--warn); font-family: "IBM Plex Sans", sans-serif; font-style: italic; }

    .ddr-prog { gap: 8px; }
    .ddr-bar { flex: 1; height: 6px; border-radius: 999px; background: var(--surface-3); overflow: hidden; }
    .ddr-bar-fill { height: 100%; border-radius: 999px; background: var(--accent); transition: width .2s; }
    .ddr-bar-done { background: var(--ok); }
    .ddr-bar-over { background: var(--bad); }
    .ddr-bar-idle { background: var(--border-strong); }
    .ddr-prog-pct { font-size: 10.5px; color: var(--fg-dim); min-width: 30px; text-align: right; }
    .ddr-prog-tag { font-size: 9.5px; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; padding: 1px 5px; border-radius: 3px; }
    .ddr-prog-over { color: var(--bad); background: var(--bad-bg); }
    .ddr-prog-done { color: var(--ok); background: var(--ok-bg); }
    .ddr-priceup { color: var(--warn); }
    .ddr-unit-delta { font-size: 9.5px; margin-left: 4px; opacity: 0.85; }

    /* ── Expanded invoice history ────────────────────────────────── */
    .ddr-detail { grid-column: 1 / -1; background: var(--surface-2); border-bottom: 1px solid var(--border); }
    .ddr-detail-inner { padding: 8px 12px 10px 40px; display: flex; flex-direction: column; gap: 4px; }
    .ddr-detail-hd { font-size: 9.5px; text-transform: uppercase; letter-spacing: 0.07em; color: var(--fg-dim); font-weight: 600; margin-bottom: 2px; }
    .ddr-bd { display: flex; align-items: center; gap: 12px; font-size: 11.5px; color: var(--fg-muted); }
    .ddr-bd-id { color: var(--fg); font-size: 10.5px; }
    .ddr-bd-date { color: var(--fg-dim); font-size: 11px; min-width: 84px; }
    .ddr-bd-qty { color: var(--fg); }
    .ddr-bd-at { color: var(--fg-dim); }
    .ddr-bd-tot { margin-left: auto; color: var(--fg); font-weight: 500; }

    /* ── Off-quote section ───────────────────────────────────────── */
    .dd-offquote { border: 1px solid var(--border); border-radius: var(--r-lg); overflow: hidden; background: var(--surface); }
    .dd-offquote-hd { display: flex; align-items: center; gap: 8px; padding: 11px 14px; border-bottom: 1px solid var(--border); background: var(--surface-2); font-size: 12.5px; font-weight: 600; }
    .dd-offquote-hd svg { color: var(--warn); }
    .dd-offquote-count { font-size: 10.5px; background: var(--warn-bg); color: var(--warn); padding: 1px 7px; border-radius: 999px; }
    .dd-offquote-note { font-weight: 400; font-size: 11.5px; color: var(--fg-dim); margin-left: 4px; }
    .dd-oq-row { display: flex; align-items: center; gap: 10px; padding: 8px 14px; border-bottom: 1px solid var(--border); font-size: 12px; }
    .dd-oq-row:last-child { border-bottom: none; }
    .dd-oq-desc { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .dd-oq-qty { color: var(--fg-dim); font-size: 11px; font-family: "IBM Plex Mono", monospace; }
    .dd-oq-amt { color: var(--fg); font-weight: 500; font-family: "IBM Plex Mono", monospace; min-width: 90px; text-align: right; }

    @keyframes dz-spin { to { transform: rotate(360deg); } }

    @media (max-width: 1000px) { .dd-kpis { grid-template-columns: repeat(2, 1fr); } }
    /* 860 (not 780): the 7-col table's intrinsic min (~792px) exceeds the card's
       content width a little below 840px — drop to the 4 core columns first. */
    @media (max-width: 860px) {
      .dd-wrap { padding: 28px 16px 48px; }
      /* Drop Progress + both unit-price columns; keep Ordered / Received / Remaining. */
      .dd-head, .ddr { grid-template-columns: minmax(140px,1fr) 64px 64px 80px; }
      .dd-h-prog, .ddr-prog { display: none; }
      .dd-h:nth-child(6), .dd-h:nth-child(7), .ddr-c:nth-child(6), .ddr-c:nth-child(7) { display: none; }
    }
  `}</style>
);

Object.assign(window, { DrawdownView });
