// views/creditnote-doc.jsx — printable credit note, same sheet as the invoice.
// Self-contained like invoice-doc.jsx: the public page at /i/<token> renders it.

function CreditNoteDoc({ inv, firm, cn }) {
  const f = firm || {};
  const rate = inv.gst_enabled ? Number(inv.gst_rate) || 0 : 0;
  const split = rate && f.gstin ? invGstSplit(inv, f, Number(cn.gst) || 0) : null;
  return (
    <div className="inv-fit">
      <div className="inv-doc">
        <div className="inv-head">
          <div className="inv-firm">
            <img className="inv-logo" src={f.logo || ((typeof window !== 'undefined' && window.__INV_ASSET_BASE__) || 'assets/') + 'luxury-unlocked-logo.png'} alt="" />
            {f.address && <div className="inv-firm-meta">{f.address}</div>}
            <div className="inv-firm-meta">
              {f.gstin && <div>GSTIN {f.gstin}</div>}
              {f.pan && <div>PAN {f.pan}</div>}
              {f.email && <div>{f.email}</div>}
            </div>
          </div>
          <div className="inv-title-block">
            <div className="inv-doctype">Credit Note</div>
            <table className="inv-meta"><tbody>
              <tr><td>Number</td><td className="mono">{cn.number}</td></tr>
              <tr><td>Date</td><td>{invDate(cn.date)}</td></tr>
              <tr><td>Against invoice</td><td className="mono">{inv.number}</td></tr>
              <tr><td>Invoice date</td><td>{invDate(inv.date)}</td></tr>
              {split && <tr><td>Place of supply</td><td>{split.pos.label || '—'}</td></tr>}
            </tbody></table>
          </div>
        </div>
        <div className="inv-party">
          <div className="inv-label">Issued to</div>
          <div className="inv-party-name">{inv.customer_name || '—'}</div>
          {inv.customer_address && <div className="inv-party-meta">{inv.customer_address}</div>}
          {inv.customer_gstin && <div className="inv-party-meta mono">GSTIN {inv.customer_gstin}</div>}
        </div>
        <table className="inv-items">
          <thead><tr><th>Description</th>{rate > 0 && <th className="w-sac">SAC</th>}<th className="w-amt">Taxable value</th></tr></thead>
          <tbody><tr>
            <td><div className="inv-item-desc">{cn.reason}{cn.note ? ' — ' + cn.note : ''}</div><div className="inv-item-detail">{inv.title || 'Travel arrangements'}</div></td>
            {rate > 0 && <td className="mono">{inv.sac_code || '998552'}</td>}
            <td className="num">{invINR(cn.taxable, { noSymbol: true })}</td>
          </tr></tbody>
        </table>
        <div className="inv-foot">
          <div className="inv-foot-left">
            <div className="inv-block"><div className="inv-label">About this note</div><div className="inv-text">This credit note reduces the amount payable on invoice {inv.number}{rate ? ', and the GST charged on it,' : ''} by {invINR(cn.gross)}.</div></div>
          </div>
          <div className="inv-foot-right">
            <table className="inv-sum"><tbody>
              <tr><td>Taxable value</td><td className="num">{invINR(cn.taxable)}</td></tr>
              {split && split.rows.map(r => <tr key={r.label}><td>{r.label} @ {r.rate}%</td><td className="num">{invINR(r.amt)}</td></tr>)}
              {!split && Number(cn.gst) > 0 && <tr><td>GST</td><td className="num">{invINR(cn.gst)}</td></tr>}
              {Number(cn.tcs) > 0 && <tr><td>TCS reversed @ {Number(inv.tcs_rate)}%</td><td className="num">{invINR(cn.tcs)}</td></tr>}
              <tr className="inv-sum-total"><td>Total credit</td><td className="num">{invINR(cn.gross)}</td></tr>
            </tbody></table>
            <div className="inv-words">{invAmountWords(cn.gross)}</div>
          </div>
        </div>
        <div className="inv-sign">
          <div className="inv-thanks"></div>
          <InvSignBlock doc={inv} firm={f} />
        </div>
      </div>
    </div>
  );
}

function cnDocPrint(inv, cn) {
  const safe = s => String(s || '').replace(/[\\/:*?"<>|]+/g, '-').replace(/\s+/g, ' ').trim();
  const prev = document.title;
  document.title = 'Credit Note ' + safe(cn.number) + ' - ' + safe(inv.customer_name);
  window.print();
  setTimeout(() => { document.title = prev; }, 1000);
}

if (typeof window !== 'undefined') Object.assign(window, { CreditNoteDoc, cnDocPrint });
