import { describe, expect, it } from "vitest";
import { defaultCsvFilename, toCsv } from "./csv";

describe("toCsv", () => {
  it("emits a header row with column labels", () => {
    const out = toCsv<{ id: string }>([], [
      { header: "Claim ID", value: (r) => r.id },
    ]);
    expect(out).toBe("Claim ID\r\n");
  });

  it("serializes primitive cell values verbatim", () => {
    const out = toCsv(
      [{ a: "hello", b: 42, c: 3.14 }],
      [
        { header: "A", value: (r) => r.a },
        { header: "B", value: (r) => r.b },
        { header: "C", value: (r) => r.c },
      ],
    );
    // 42 and 3.14 both serialize without quotes, no locale formatting.
    expect(out).toBe("A,B,C\r\nhello,42,3.14\r\n");
  });

  it("quotes fields containing commas", () => {
    const out = toCsv([{ name: "Smith, John" }], [
      { header: "Name", value: (r) => r.name },
    ]);
    expect(out).toBe('Name\r\n"Smith, John"\r\n');
  });

  it("escapes embedded quotes by doubling them", () => {
    const out = toCsv([{ note: 'she said "hi"' }], [
      { header: "Note", value: (r) => r.note },
    ]);
    expect(out).toBe('Note\r\n"she said ""hi"""\r\n');
  });

  it("quotes fields containing newlines", () => {
    const out = toCsv([{ note: "line1\nline2" }], [
      { header: "Note", value: (r) => r.note },
    ]);
    expect(out).toBe('Note\r\n"line1\nline2"\r\n');
  });

  it("quotes fields containing carriage returns", () => {
    const out = toCsv([{ note: "line1\rline2" }], [
      { header: "Note", value: (r) => r.note },
    ]);
    expect(out).toBe('Note\r\n"line1\rline2"\r\n');
  });

  it("renders null and undefined as empty cells", () => {
    const out = toCsv(
      [{ a: null, b: undefined, c: "kept" }],
      [
        { header: "A", value: (r) => r.a },
        { header: "B", value: (r) => r.b },
        { header: "C", value: (r) => r.c },
      ],
    );
    expect(out).toBe("A,B,C\r\n,,kept\r\n");
  });

  it("renders an empty-string cell as a bare empty field", () => {
    const out = toCsv([{ a: "" }], [{ header: "A", value: (r) => r.a }]);
    expect(out).toBe("A\r\n\r\n");
  });

  it("serializes multiple rows separated by CRLF", () => {
    const out = toCsv(
      [{ id: 1 }, { id: 2 }, { id: 3 }],
      [{ header: "id", value: (r) => r.id }],
    );
    expect(out).toBe("id\r\n1\r\n2\r\n3\r\n");
  });

  it("quotes a header that itself contains a comma", () => {
    const out = toCsv([], [{ header: "Last, First", value: () => "" }]);
    expect(out).toBe('"Last, First"\r\n');
  });

  it("forces a string around non-string inputs (booleans, dates)", () => {
    const out = toCsv(
      [{ on: true as const, at: new Date("2024-01-02T03:04:05.000Z") }],
      [
        { header: "On", value: (r) => String(r.on) },
        { header: "At", value: (r) => r.at.toISOString() },
      ],
    );
    expect(out).toBe("On,At\r\ntrue,2024-01-02T03:04:05.000Z\r\n");
  });

  it("returns only the header when rows is empty (no trailing body CRLF doubled)", () => {
    const out = toCsv<{ id: string }>([], [
      { header: "ID", value: (r) => r.id },
      { header: "Name", value: () => "x" },
    ]);
    expect(out).toBe("ID,Name\r\n");
  });
});

describe("defaultCsvFilename", () => {
  it("formats a prefix + ISO date suffix as .csv", () => {
    const fixed = new Date("2024-03-05T22:00:00.000Z");
    expect(defaultCsvFilename("claims", fixed)).toBe("claims-2024-03-05.csv");
  });
});