/**
 * Shipped starting point for each printable document. Seeded on first use and
 * restorable from the editor, so a template that gets mangled is one click from
 * working again. Everything here is data the back office may rewrite.
 *
 * A template is body content (plus its own <style>), not a whole HTML file — the
 * renderer wraps it in a skeleton and appends the evidence pages after it.
 */

export const INVOICE_TEMPLATE_NAME = 'Debit Note';

/** Modelled on the Well Faith debit note: letterhead, To/Property blocks, a
 * ruled particulars table with a Less: section, then the payment footnotes. */
export const INVOICE_TEMPLATE_BODY = `<style>
  .dn { font-family: "Times New Roman", "Songti SC", serif; color: #000; font-size: 11pt; }
  .dn .letterhead { font-size: 17pt; font-weight: 700; }
  .dn .subhead { display: flex; justify-content: space-between; gap: 6mm; align-items: flex-start; font-size: 7.5pt; font-style: italic;
                 border-bottom: 1px solid #000; padding-bottom: 2mm; }
  .dn .subhead .contact { text-align: right; line-height: 1.45; }
  .dn .refs { margin-top: 8mm; text-align: right; line-height: 1.7; }
  .dn .refs b { display: inline-block; min-width: 18mm; text-align: right; font-weight: 400; }
  .dn h1 { text-align: center; font-size: 15pt; font-style: italic; letter-spacing: .18em; margin: 7mm 0 9mm; }
  .dn .parties { display: flex; gap: 10mm; margin-bottom: 8mm; }
  .dn .parties > div { flex: 1; }
  .dn .parties .lbl { font-weight: 700; }
  .dn .parties table td { padding: 0 4mm 0 0; vertical-align: top; }
  .dn table.items { width: 100%; border-collapse: collapse; }
  .dn table.items th, .dn table.items td { border: 1px solid #000; padding: 1.5mm 2mm; }
  .dn table.items thead th { text-align: center; font-weight: 700; }
  .dn table.items .c-desc { width: 55%; }
  .dn table.items .c-from, .dn table.items .c-to { width: 13%; text-align: center; white-space: nowrap; }
  .dn table.items .c-amt { width: 19%; text-align: right; white-space: nowrap; }
  .dn table.items tbody td { border-top: 0; border-bottom: 0; vertical-align: top; }
  .dn table.items tbody tr.first td { border-top: 1px solid #000; }
  .dn .section { text-decoration: underline; }
  .dn .note { padding-left: 4mm; font-size: 9pt; color: #333; }
  .dn .filler td { height: 26mm; }
  .dn table.items tfoot td { border-top: 1px solid #000; font-weight: 700; }
  .dn .foot { margin-top: 7mm; font-size: 9pt; line-height: 1.6; }
  .dn .foot ol { margin: 0; padding-left: 5mm; }
  .dn .foot .bank { margin: 1mm 0 2mm 4mm; }
  .dn .foot .bank td { padding-right: 4mm; }
  .dn .payee { font-size: 12pt; font-weight: 700; }
</style>

<div class="dn">
  <div class="letterhead">{{org.name}}</div>
  <div class="subhead">
    <span>{{org.address}}</span>
    <span class="contact">{{#if org.tel}}Tel : {{org.tel}}{{/if}}{{#if org.fax}} &nbsp; Fax : {{org.fax}}{{/if}}{{#if org.email}}<br>Email : {{org.email}}{{/if}}</span>
  </div>

  <div class="refs">
    <div><b>D/N No :</b> &nbsp;{{doc.number}}</div>
    <div><b>Date :</b> &nbsp;{{dateShort doc.date}}</div>
  </div>

  <h1>DEBIT NOTE</h1>

  <div class="parties">
    <div>
      <div class="lbl">To :</div>
      <div>{{tenant.name}}</div>{{#if tenant.address}}<div style="white-space:pre-line">{{tenant.address}}</div>{{/if}}
      <table>
        {{#if tenant.attention}}<tr><td>Attn :</td><td>{{tenant.attention}}</td></tr>{{/if}}
        {{#if tenant.phone}}<tr><td>Tel No. :</td><td>{{tenant.phone}}</td></tr>{{/if}}{{#if tenant.email}}<tr><td>Email :</td><td>{{tenant.email}}</td></tr>{{/if}}
      </table>
    </div>
    <div>
      <div class="lbl">Property :</div>
      {{#each property.addressLines}}<div>{{this}}</div>{{/each}}
    </div>
  </div>

  <table class="items">
    <thead>
      <tr>
        <th rowspan="2" class="c-desc">Particulars</th>
        <th colspan="2">Period</th>
        <th rowspan="2" class="c-amt">Amount<br>({{doc.currency}})</th>
      </tr>
      <tr><th class="c-from">From</th><th class="c-to">To</th></tr>
    </thead>
    <tbody>
      <tr class="first"><td class="section">Current Month Transactions:</td><td></td><td></td><td></td></tr>
      {{#each groups}}
        {{#unless negative}}
        <tr>
          <td>
            {{description}}{{#if reading}} ({{reading}}){{/if}}
            {{!-- Kept inside the cell: a colspan row would break the column rules --}}
            {{#if units}}<div class="note">Meter {{serial}} — units {{units}}{{#if deduction}} · {{deduction}}{{/if}}</div>{{/if}}
          </td>
          <td class="c-from">{{date from}}</td>
          <td class="c-to">{{date to}}</td>
          <td class="c-amt">{{money amount}}</td>
        </tr>
        {{/unless}}
      {{/each}}
      {{#if credits.length}}
        <tr><td class="section">Less :</td><td></td><td></td><td></td></tr>
        {{#each groups}}
          {{#if negative}}
          <tr>
            <td>{{description}}</td>
            <td class="c-from">{{date from}}</td>
            <td class="c-to">{{date to}}</td>
            <td class="c-amt">{{money amount}}</td>
          </tr>
          {{/if}}
        {{/each}}
      {{/if}}
      <tr class="filler"><td></td><td></td><td></td><td></td></tr>
    </tbody>
    <tfoot>
      <tr><td></td><td colspan="2" style="text-align:center">Total :</td><td class="c-amt">{{money total}}</td></tr>
    </tfoot>
  </table>

  <div class="foot">
    <ol>
      <li>
        Please pay by crossed cheque made payable to <span class="payee">{{payee.accountName}}</span>.
        <div>Our bank information as below:</div>
        <table class="bank">
          <tr><td>Bank Name:</td><td>{{payee.bankName}}</td></tr>
          <tr><td>Account Name:</td><td>{{payee.accountName}}</td></tr>
          <tr><td>Account Number:</td><td>{{payee.accountNumber}}</td></tr>
        </table>
      </li>
      <li>In case of late payment, a penalty interest charge will be applied by the Tenancy Agreement.</li>
      <li>Official receipt will be issued on request.</li>
    </ol>
  </div>
</div>`;

export const STATEMENT_TEMPLATE_NAME = 'Monthly Statement';

/** Modelled on the ERP's monthly statement: letterhead on the right, the tenant
 * and the period, every document with the balance running down, then what is
 * owed by age and the deposit and unused advance payment held. One per lease;
 * a run of them prints one after another, each from a new page. */
export const STATEMENT_TEMPLATE_BODY = `<style>
  .st { font-family: Arial, "Helvetica Neue", "PingFang HK", "Microsoft JhengHei", sans-serif; color: #000; font-size: 9.5pt; }
  .st .head { text-align: right; line-height: 1.35; }
  .st .head .name { font-size: 16pt; font-weight: 700; }
  .st .head .name-zh { font-size: 15pt; font-weight: 700; }
  .st .head .small { font-size: 8.5pt; white-space: pre-line; }
  .st h1 { text-align: center; font-size: 13pt; font-weight: 400; margin: 6mm 0 3mm; }
  .st .to { border-collapse: collapse; }
  .st .to td { padding: 0.4mm 2mm 0.4mm 0; vertical-align: top; }
  .st .to td:first-child { width: 24mm; }
  .st table.tx { width: 100%; border-collapse: collapse; margin-top: 3mm; }
  .st table.tx th { font-weight: 400; text-align: left; vertical-align: bottom; border-top: 1px solid #000; border-bottom: 1px solid #000; padding: 1mm; }
  .st table.tx td { padding: 1mm; vertical-align: top; }
  .st table.tx tr { break-inside: avoid; }
  .st .r { text-align: right; white-space: nowrap; }
  .st .nowrap { white-space: nowrap; }
  .st .balance { display: flex; justify-content: flex-end; gap: 6mm; margin-top: 1mm; }
  .st .balance .amt { min-width: 38mm; text-align: right; border-top: 1px solid #000; border-bottom: 3px double #000; padding: 0.5mm 0; }
  .st table.aging { width: 100%; border-collapse: collapse; margin-top: 6mm; }
  .st table.aging th { font-weight: 400; text-align: right; border-top: 1px solid #000; border-bottom: 1px solid #000; padding: 1mm; }
  .st table.aging td { text-align: right; padding: 1mm; border-bottom: 1px solid #000; }
  .st table.aging th:first-child, .st table.aging td:first-child { text-align: left; }
  .st table.held { margin-top: 3mm; border-collapse: collapse; }
  .st table.held td { padding: 0.4mm 3mm 0.4mm 0; }
</style>

<div class="st">
  <div class="head">
    <div class="name">{{org.name}}</div>
    {{#if org.nameZh}}<div class="name-zh">{{org.nameZh}}</div>{{/if}}
    {{#if org.address}}<div class="small">{{org.address}}</div>{{/if}}
    <div class="small">{{#if org.tel}}Tel.: {{org.tel}}{{/if}}{{#if org.fax}} &nbsp;Fax: {{org.fax}}{{/if}}</div>
  </div>

  <h1>Monthly Statement</h1>

  <table class="to">
    <tr><td>Messrs.</td><td>:</td><td>{{tenant.name}}{{#if tenant.code}} ({{tenant.code}}){{/if}}</td></tr>
    {{#if tenant.attention}}<tr><td>Attn</td><td>:</td><td>{{tenant.attention}}</td></tr>{{/if}}
    <tr><td>Address</td><td>:</td><td>{{#each property.addressLines}}<div>{{this}}</div>{{/each}}</td></tr>
    <tr><td>Lease</td><td>:</td><td>{{lease.reference}}</td></tr>
    <tr><td>Date</td><td>:</td><td>{{date statement.from}} To {{date statement.to}}</td></tr>
  </table>

  <table class="tx">
    <thead>
      <tr>
        <th>Document</th>
        <th>Date</th>
        <th>Description</th>
        <th class="r">Amount<br>({{statement.currency}})</th>
        <th class="r">Debit<br>({{statement.currency}})</th>
        <th class="r">Credit<br>({{statement.currency}})</th>
        <th class="r">Balance<br>({{statement.currency}})</th>
      </tr>
    </thead>
    <tbody>
      {{#each rows}}
      <tr>
        <td class="nowrap">{{number}}</td>
        <td class="nowrap">{{date date}}</td>
        <td>{{description}}</td>
        <td class="r">{{#if amount}}{{money amount}}{{/if}}</td>
        <td class="r">{{#if debit}}{{money debit}}{{/if}}</td>
        <td class="r">{{#if credit}}{{money credit}}{{/if}}</td>
        <td class="r">{{money balance}}</td>
      </tr>
      {{/each}}
    </tbody>
  </table>
  <div class="balance"><span>Balance : &nbsp;{{statement.currency}}</span><span class="amt">{{money closing}}</span></div>

  <table class="aging">
    <tr><th></th><th>Not Yet Due</th><th>0 - 30 Days</th><th>31 - 60 Days</th><th>61 - 90 Days</th><th>Over 90 Days</th></tr>
    <tr>
      <td>{{statement.currency}}</td>
      <td>{{money aging.notYetDue}}</td>
      <td>{{money aging.d0_30}}</td>
      <td>{{money aging.d31_60}}</td>
      <td>{{money aging.d61_90}}</td>
      <td>{{money aging.over90}}</td>
    </tr>
  </table>

  <table class="held">
    <tr><td>Deposit Remained</td><td>: {{statement.currency}}</td><td class="r">{{money deposit}}</td></tr>
    <tr><td>Unallocated Amount</td><td>: {{statement.currency}}</td><td class="r">{{money unallocated}}</td></tr>
  </table>
</div>`;

export const RECEIPT_TEMPLATE_NAME = 'Receipt';

/** Modelled on the ERP's receipt: letterhead on the right, the tenant beside
 * the receipt's number and date, the debit notes it settled with the total in
 * words, then what was received and how. A deposit receipt pays no note yet,
 * so a line saying what it is for stands where the notes would. The payment
 * proof, when there is one, follows on pages of its own. */
export const RECEIPT_TEMPLATE_BODY = `<style>
  .rc { position: relative; min-height: 262mm; font-family: Arial, "Helvetica Neue", "PingFang HK", "Microsoft JhengHei", sans-serif; color: #000; font-size: 10pt; }
  .rc .head { text-align: right; line-height: 1.35; }
  .rc .head .name { font-size: 17pt; font-weight: 700; }
  .rc .head .name-zh { font-size: 15pt; font-weight: 700; }
  .rc .head .small { font-size: 8.5pt; white-space: pre-line; }
  .rc h1 { text-align: center; font-size: 15pt; font-weight: 400; margin: 12mm 0 12mm; }
  .rc .parties { display: flex; gap: 6mm; }
  .rc .parties > div:first-child { flex: 1.3; }
  .rc .parties > div:last-child { flex: 1; }
  .rc .parties td { padding: 0.3mm 2mm 0.3mm 0; vertical-align: top; }
  .rc .nowrap { white-space: nowrap; }
  .rc .lead { margin-top: 7mm; border-bottom: 1px solid #000; padding-bottom: 0.5mm; }
  .rc table.inv { width: 100%; border-collapse: collapse; }
  .rc table.inv th { font-weight: 400; text-align: left; border-bottom: 1px solid #000; padding: 1mm 2mm; }
  .rc table.inv td { padding: 1mm 2mm; }
  .rc .r, .rc table.inv th.r { text-align: right; white-space: nowrap; }
  .rc table.inv .c { width: 4mm; padding-left: 0; padding-right: 0; }
  .rc table.inv .sym { width: 12mm; }
  .rc .u { border-bottom: 1px solid #000; }
  .rc .uu { border-bottom: 3px double #000; }
  .rc .words { margin-top: 1mm; }
  .rc table.paid { border-collapse: collapse; }
  .rc table.paid td { padding: 0.3mm 3mm 0.3mm 0; vertical-align: top; }
  .rc .stamp { position: absolute; top: 135mm; left: 50%; transform: translateX(-50%) rotate(-12deg); border: 3px solid #b91c1c;
               color: #b91c1c; font-size: 26pt; font-weight: 700; letter-spacing: .12em; padding: 2mm 6mm; opacity: .85; }
  .rc .sign { position: absolute; bottom: 0; left: 10mm; line-height: 1.6; }
  .rc .sign .dept { width: 50mm; border-bottom: 1px solid #000; }
</style>

<div class="rc">
  <div class="head">
    <div class="name">{{org.name}}</div>
    {{#if org.nameZh}}<div class="name-zh">{{org.nameZh}}</div>{{/if}}
    {{#if org.address}}<div class="small">{{org.address}}</div>{{/if}}
    <div class="small">{{#if org.tel}}Tel.: {{org.tel}}{{/if}}{{#if org.fax}} &nbsp;Fax: {{org.fax}}{{/if}}</div>
  </div>

  <h1>Receipt</h1>
  {{#if receipt.dishonoured}}<div class="stamp">DISHONOURED</div>{{/if}}

  <div class="parties">
    <div>
      <table>
        <tr><td class="nowrap">Messr.:</td><td>{{tenant.name}}{{#each property.addressLines}}<div>{{this}}</div>{{/each}}</td></tr>
        {{#if tenant.attention}}<tr><td class="nowrap">Attn.</td><td>: {{tenant.attention}}</td></tr>{{/if}}
      </table>
    </div>
    <div>
      <table>
        <tr><td class="nowrap">Receipt No.</td><td>: {{receipt.number}}</td></tr>
        <tr><td>Date</td><td>: {{dateCompact receipt.date}}</td></tr>
        {{#if tenant.code}}<tr><td>Customer</td><td>: {{tenant.code}}</td></tr>{{/if}}
        {{#if tenant.phone}}<tr><td>Tel.</td><td>: {{tenant.phone}}</td></tr>{{/if}}
      </table>
    </div>
  </div>

  {{#if advance}}
  <div class="lead">Being payment received in advance for Lease {{lease.reference}}</div>
  <table class="inv">
    <tr><td style="width:46%"></td><td>Total Amount</td><td class="c">:</td><td class="r uu sym">{{receipt.symbol}}</td><td class="r uu" style="width:28mm">{{money total}}</td></tr>
  </table>
  {{else}}
  <div class="lead">Being in full/partial settlement of the following invoices:</div>
  <table class="inv">
    <thead><tr><th style="width:22%">Invoice No.</th><th style="width:24%">Date</th><th></th><th class="c"></th><th colspan="2" class="r">Amount</th></tr></thead>
    <tbody>
      {{#each invoices}}
      <tr><td>{{number}}</td><td>{{dateCompact date}}</td><td></td><td class="c"></td><td class="r sym">{{../receipt.symbol}}</td><td class="r" style="width:28mm">{{money amount}}</td></tr>
      {{/each}}
      <tr><td></td><td></td><td>Settled Amount</td><td class="c">:</td><td class="r u">{{receipt.symbol}}</td><td class="r u">{{money total}}</td></tr>
      <tr><td></td><td></td><td>Total Amount</td><td class="c">:</td><td class="r uu">{{receipt.symbol}}</td><td class="r uu">{{money total}}</td></tr>
    </tbody>
  </table>
  {{/if}}

  <div class="words">SAY TOTAL {{words}}</div>

  <table class="paid">
    {{#unless advance}}<tr><td>Amount Applied</td><td>:</td><td>{{receipt.symbol}}</td><td class="r">{{money applied}}</td><td colspan="3"></td></tr>{{/unless}}
    <tr>
      <td>Actual Receipt</td><td>:</td><td>{{receipt.symbol}}</td><td class="r">{{money actual}}</td>
      {{#if receipt.reference}}<td style="padding-left:12mm">{{#if receipt.method}}{{receipt.method}} {{/if}}No.</td><td>:</td><td>{{receipt.reference}}</td>{{/if}}
    </tr>
    {{#each excess}}<tr><td>Excess</td><td>:</td><td>{{../receipt.symbol}}</td><td class="r">{{money amount}}</td><td colspan="3" style="padding-left:12mm">held as advance payment under {{number}}</td></tr>{{/each}}
    {{#if receipt.remark}}<tr><td>Remarks</td><td>:</td><td colspan="5">{{receipt.remark}}</td></tr>{{/if}}
  </table>

  <div class="sign">
    <div class="dept">A/C Dept</div>
    <div>Prepared By</div>
    <div>{{org.name}}</div>
  </div>
</div>`;

/** The shipped template for each document kind. */
export const DEFAULT_TEMPLATES: Record<string, { name: string; body: string }> = {
  INVOICE: { name: INVOICE_TEMPLATE_NAME, body: INVOICE_TEMPLATE_BODY },
  STATEMENT: { name: STATEMENT_TEMPLATE_NAME, body: STATEMENT_TEMPLATE_BODY },
  RECEIPT: { name: RECEIPT_TEMPLATE_NAME, body: RECEIPT_TEMPLATE_BODY },
};
