function PrintReport({ deal, calc }) {
  const p = deal.property;
  const t = deal.terms;
  const today = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
  const nz = (arr) => arr.filter((x) => Number(x.v) > 0);
  const estAvg = (() => { const v = deal.estimates.map((e) => Number(e.v) || 0).filter((x) => x > 0); return v.length ? v.reduce((a, b) => a + b, 0) / v.length : 0; })();
  const good = calc.netProfit >= calc.profitGoal;
  const mv = deal.market.value, mr = deal.market.rent;
  const offer = computeOffer(deal, calc);

  const Tbl = ({ title, rows, total, totalLabel }) => (
    <div className="pr-card">
      <div className="pr-card-h">{title}<span>{fmtMoney(total)}</span></div>
      <table className="pr-tbl">
        <tbody>
          {rows.map((r, i) => (
            <tr key={i}><td>{r.k}</td><td>{fmtMoney(r.v)}</td></tr>
          ))}
          {totalLabel ? <tr className="pr-total"><td>{totalLabel}</td><td>{fmtMoney(total)}</td></tr> : null}
        </tbody>
      </table>
    </div>
  );

  return (
    <div className="print-report">
      <div className="pr-head">
        <div className="pr-brand">
          <span className="pr-logo">▲</span> Flip Ledger
          <span className="pr-sub">Investment Analysis</span>
        </div>
        <div className="pr-date">Prepared {today}</div>
      </div>

      <div className="pr-address">
        {p.photoUrl ? <img src={p.photoUrl} alt="" className="pr-photo" /> : null}
        <div>
          <h1>{p.address}</h1>
          <div className="pr-loc">{p.city}, {p.state} {p.zip}{p.mls && p.mls !== '—' ? ' · MLS ' + p.mls : ''}</div>
          <div className="pr-specs">{p.beds} bd&nbsp;·&nbsp;{p.baths} ba&nbsp;·&nbsp;{fmtNum(p.sqft)} ft²&nbsp;·&nbsp;{p.lotSize} ac lot&nbsp;·&nbsp;{t.purchaseType}</div>
        </div>
        <div className="pr-headline">
          <div className="pr-hl-l">Projected Net Profit</div>
          <div className={'pr-hl-v' + (calc.netProfit < 0 ? ' neg' : '')}>{fmtMoney(calc.netProfit)}</div>
          <div className="pr-hl-s">{fmtPct(calc.netMargin)} margin · {fmtPct(calc.roi)} ROI</div>
        </div>
      </div>

      <div className="pr-metrics">
        {[
          ['Purchase Price', fmtMoney(t.acceptedPrice)],
          ['Purchase Costs', fmtMoney(calc.purchaseFeesSum)],
          ['Rehab Budget', fmtMoney(calc.rehabTotal)],
          ['ARV / Sale', fmtMoney(t.sellingPrice)],
          ['Selling Costs', fmtMoney(calc.sellingFeesSum)],
          ['All-In Cost', fmtMoney(calc.allInCost)],
        ].map((m, i) => (
          <div className="pr-metric" key={i}><div className="pm-l">{m[0]}</div><div className="pm-v">{m[1]}</div></div>
        ))}
      </div>

      <div className="pr-cols">
        <Tbl title="Purchase" total={calc.purchaseTotal} totalLabel="Purchase Total"
          rows={[{ k: 'Accepted Price', v: t.acceptedPrice }, ...nz(deal.purchaseFees)]} />
        <Tbl title="Sale" total={calc.netSalePrice} totalLabel="Net Sale Price"
          rows={[{ k: 'Selling Price (ARV)', v: t.sellingPrice }, ...nz(deal.sellingFees).map((r) => ({ k: r.k, v: r.v }))]} />
      </div>

      <div className="pr-cols">
        <Tbl title="Rehab Scope" total={calc.rehabTotal} totalLabel="Rehab Total" rows={nz(deal.rehab)} />
        <div className="pr-card">
          <div className="pr-card-h">Key Ratios</div>
          <table className="pr-tbl">
            <tbody>
              <tr><td>Return on Investment</td><td>{fmtPct(calc.roi)}</td></tr>
              <tr><td>Net Profit Margin</td><td>{fmtPct(calc.netMargin)}</td></tr>
              <tr><td>Profit Goal ({t.profitGoalPct}%)</td><td>{fmtMoney(calc.profitGoal)}</td></tr>
              <tr><td>Holding Cost ({t.holdingMonths} mo)</td><td>{fmtMoney(calc.holdingCost)}</td></tr>
              <tr><td>Max Offer · 70% Rule</td><td>{fmtMoney(calc.mao)}</td></tr>
              <tr><td>Blended AVM Estimate</td><td>{fmtMoney(estAvg)}</td></tr>
              <tr className="pr-total"><td>{good ? 'Meets profit goal' : 'Below profit goal'}</td><td>{good ? '✓' : '—'}</td></tr>
            </tbody>
          </table>
        </div>
      </div>

      <div className="pr-cols">
        <div className="pr-card">
          <div className="pr-card-h">Market Value Estimate<span>{mv.source === 'live' ? 'RentCast' : 'Sample'}</span></div>
          <table className="pr-tbl">
            <tbody>
              <tr><td>Low</td><td>{fmtMoney(mv.low)}</td></tr>
              <tr><td>Median (AVM)</td><td>{fmtMoney(mv.mid)}</td></tr>
              <tr><td>High</td><td>{fmtMoney(mv.high)}</td></tr>
              <tr className="pr-total"><td>Offer for {t.profitGoalPct}% goal</td><td>{fmtMoney(offer.atGoal)}</td></tr>
            </tbody>
          </table>
        </div>
        <div className="pr-card">
          <div className="pr-card-h">Rent Estimate (mo)<span>{mr.source === 'live' ? 'RentCast' : 'Sample'}</span></div>
          <table className="pr-tbl">
            <tbody>
              <tr><td>Low</td><td>{fmtMoney(mr.low)}</td></tr>
              <tr><td>Median</td><td>{fmtMoney(mr.mid)}</td></tr>
              <tr><td>High</td><td>{fmtMoney(mr.high)}</td></tr>
              <tr className="pr-total"><td>Gross annual rent</td><td>{fmtMoney(mr.mid * 12)}</td></tr>
            </tbody>
          </table>
        </div>
      </div>

      <div className="pr-foot">
        Flip Ledger · estimates only, not financial advice. AVM &amp; rent figures are user-supplied / sample data and should be verified against the live listing and a local market analysis before investment.
      </div>
    </div>
  );
}

window.PrintReport = PrintReport;
