import { describe, expect, it } from 'vitest';
import { INVOICE_TEMPLATE_BODY } from './default-templates';
import { printedParticulars, withCompanyEmail, withTenantContact } from './debit-note-print';

describe('printed particulars', () => {
  it('drops the billing month, which is not the line coverage', () => {
    expect(printedParticulars('Government Rates · 2026-10')).toBe('Government Rates');
    expect(printedParticulars('Rent · 2026-02 — rent-free 2026-02-01 → 2026-02-15 (15 day(s) not charged)')).toBe(
      'Rent — rent-free 2026-02-01 → 2026-02-15 (15 day(s) not charged)',
    );
  });

  it('leaves a description that has no billing month', () => {
    expect(printedParticulars('Balance b/f from DN-1001')).toBe('Balance b/f from DN-1001');
  });
});

describe('company email on an existing debit-note template', () => {
  const legacy = INVOICE_TEMPLATE_BODY.replace(
    '<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>',
    '<span>{{#if org.tel}}Tel : {{org.tel}}{{/if}}{{#if org.fax}} &nbsp; Fax : {{org.fax}}{{/if}}</span>',
  ).replace(
    `.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 .subhead { display: flex; justify-content: space-between; font-size: 7.5pt; font-style: italic;
                 border-bottom: 1px solid #000; padding-bottom: 2mm; }`,
  );

  it('adds Email under Tel on the old letterhead line', () => {
    const next = withCompanyEmail(legacy);
    expect(next).toContain('Email : {{org.email}}');
    expect(next).toContain('class="contact"');
    expect(next).toContain('.subhead .contact');
  });

  it('leaves a template that already prints the email', () => {
    expect(withCompanyEmail(INVOICE_TEMPLATE_BODY)).toBe(INVOICE_TEMPLATE_BODY);
  });

  it('leaves a template the office has rewritten', () => {
    const custom = '<div>Tel : {{org.tel}}</div>';
    expect(withCompanyEmail(custom)).toBe(custom);
  });
});

describe('tenant address and email on an existing debit-note template', () => {
  const legacy = INVOICE_TEMPLATE_BODY.replace(
    '{{#if tenant.address}}<div style="white-space:pre-line">{{tenant.address}}</div>{{/if}}',
    '',
  ).replace('{{#if tenant.email}}<tr><td>Email :</td><td>{{tenant.email}}</td></tr>{{/if}}', '');

  it('brings an old template to the shipped one', () => {
    expect(legacy).not.toContain('tenant.address');
    expect(withTenantContact(legacy)).toBe(INVOICE_TEMPLATE_BODY);
  });

  it('leaves a template that already prints them', () => {
    expect(withTenantContact(INVOICE_TEMPLATE_BODY)).toBe(INVOICE_TEMPLATE_BODY);
  });

  it('leaves a template the office has rewritten', () => {
    const custom = '<p>{{tenant.name}}</p><p>Tel {{tenant.phone}}</p>';
    expect(withTenantContact(custom)).toBe(custom);
  });
});
