import { describe, expect, it } from "vitest";
import {
  buildHanmacImportEmailPreview,
  buildKoreanNameEmailBase,
  matchesSuggestedNameRule,
} from "./hanmacImportEmail";

describe("hanmac import email policy", () => {
  it("builds name initials plus surname base", () => {
    expect(buildKoreanNameEmailBase("한치영")).toEqual({
      base: "cyhan",
      needsReview: false,
    });
  });

  it("matches base plus numeric suffix only", () => {
    expect(matchesSuggestedNameRule("cyhan", "cyhan")).toBe(true);
    expect(matchesSuggestedNameRule("cyhan2", "cyhan")).toBe(true);
    expect(matchesSuggestedNameRule("hcy", "cyhan")).toBe(false);
    expect(matchesSuggestedNameRule("cyhan-a", "cyhan")).toBe(false);
  });

  it("suggests the next available local part for domain-only email", () => {
    const preview = buildHanmacImportEmailPreview(
      {
        email: "@hanmaceng.co.kr",
        name: "한치영",
        tenantSlug: "hanmac",
        metadata: {},
      },
      new Set(["cyhan", "cyhan1"]),
      new Set(),
    );

    expect(preview.finalEmail).toBe("cyhan2@hanmaceng.co.kr");
    expect(preview.status).toBe("suggested");
    expect(preview.warnings).toContain("suggested");
  });

  it("marks rule mismatch as a warning without blocking the row", () => {
    const preview = buildHanmacImportEmailPreview(
      {
        email: "hcy@hanmaceng.co.kr",
        name: "한치영",
        tenantSlug: "hanmac",
        metadata: {},
      },
      new Set(),
      new Set(),
    );

    expect(preview.finalEmail).toBe("hcy@hanmaceng.co.kr");
    expect(preview.status).toBe("ruleMismatch");
    expect(preview.warnings).toContain("ruleMismatch");
    expect(preview.blockingErrors).toEqual([]);
  });

  it("blocks duplicate full local part for Hanmac family", () => {
    const preview = buildHanmacImportEmailPreview(
      {
        email: "han@samaneng.com",
        name: "한치영",
        tenantSlug: "hanmac",
        metadata: {},
      },
      new Set(["han"]),
      new Set(),
    );

    expect(preview.status).toBe("blockingError");
    expect(preview.blockingErrors).toContain("duplicateLocalPart");
  });
});
