import { describe, it, expect } from "vitest";
import { mapImportRow } from "./map-import-row";

const mapping = { email: "E-mail", name: "Nome", phone: "Telefone", tags: "Tags" };

describe("mapImportRow", () => {
  it("maps a valid row into a normalized contact", () => {
    const result = mapImportRow(
      { "E-mail": "Maria@Example.com", Nome: "Maria Silva", Telefone: "11999999999", Tags: "vip, evento" },
      mapping
    );

    expect(result.valid).toBe(true);
    if (result.valid) {
      expect(result.data).toEqual({
        email: "maria@example.com",
        name: "Maria Silva",
        phone: "11999999999",
        tags: ["vip", "evento"],
      });
    }
  });

  it("rejects a row with an invalid e-mail", () => {
    const result = mapImportRow({ "E-mail": "not-an-email", Nome: "Maria" }, mapping);

    expect(result.valid).toBe(false);
    if (!result.valid) {
      expect(result.error).toContain("inválido");
    }
  });

  it("rejects a row with an empty e-mail", () => {
    const result = mapImportRow({ "E-mail": "   ", Nome: "Maria" }, mapping);

    expect(result.valid).toBe(false);
    if (!result.valid) {
      expect(result.error).toContain("vazio");
    }
  });

  it("works with only the e-mail column mapped (name/phone/tags optional)", () => {
    const result = mapImportRow({ "E-mail": "contato@example.com" }, { email: "E-mail" });

    expect(result.valid).toBe(true);
    if (result.valid) {
      expect(result.data).toEqual({ email: "contato@example.com", name: undefined, phone: undefined, tags: [] });
    }
  });
});
