import type { RenewalLetterRow } from "../reports/renewal-letter";
import { renderRenewalEmail } from "./renewal-email";

function letter(overrides: Partial<RenewalLetterRow> = {}): RenewalLetterRow {
  return {
    __kind: "letter",
    policyId: "policy-1",
    policyNumber: "POL-123",
    policyType: "AUTO",
    customerName: "Ana Pérez",
    customerEmail: "ana@example.com",
    customerPhone: "664-111-2222",
    customerMobile: null,
    customerAddress: ["Calle Uno 123", "Tijuana, BC, 22000"],
    provider: "Aseguradora Uno",
    policyTo: "2026-09-01",
    netPremium: "1200.00",
    policyFee: null,
    total: "1392.00",
    currency: "MXN",
    coverageDays: null,
    cslLimit: null,
    medicalCoverage: null,
    propertyDamage: null,
    perPersonLiability: null,
    additionalService: null,
    vehicle: null,
    generation: 1,
    sentAt: null,
    ...overrides,
  };
}

describe("renderRenewalEmail", () => {
  it("includes policy, premium, expiration, type, and customer information", () => {
    const result = renderRenewalEmail(letter());

    expect(result.subject).toContain("POL-123");
    expect(result.html).toContain("primer aviso");
    expect(result.html).toContain("AUTO");
    expect(result.html).toContain("01/09/2026");
    expect(result.html).toContain("1,392.00");
    expect(result.html).toContain("Ana Pérez");
    expect(result.html).toContain("ana@example.com");
    expect(result.html).toContain("664-111-2222");
    expect(result.html).toContain("Calle Uno 123");
  });

  it("uses overdue wording for generation three", () => {
    const result = renderRenewalEmail(letter({ generation: 3 }));

    expect(result.subject).toContain("Póliza vencida");
    expect(result.html).toContain("está vencida");
  });

  it("escapes customer-provided HTML", () => {
    const result = renderRenewalEmail(
      letter({ customerName: '<img src=x onerror="alert(1)">' }),
    );

    expect(result.html).not.toContain("<img");
    expect(result.html).toContain("&lt;img");
  });
});
