import { describe, expect, it } from "vitest";
import {
  findDomainConflict,
  formatDomainConflictMessage,
  normalizeDomainTokens,
} from "./domainTags";

describe("domainTags", () => {
  it("splits domains by comma and whitespace", () => {
    expect(
      normalizeDomainTokens("samaneng.com, hanmaceng.co.kr login.hmac.kr"),
    ).toEqual(["samaneng.com", "hanmaceng.co.kr", "login.hmac.kr"]);
  });

  it("finds a domain already assigned to another tenant", () => {
    const conflict = findDomainConflict("hanmaceng.co.kr", [
      {
        id: "tenant-1",
        name: "한맥기술",
        slug: "hanmac",
        type: "COMPANY",
        description: "",
        status: "active",
        domains: ["hanmaceng.co.kr"],
        memberCount: 0,
        createdAt: "",
        updatedAt: "",
      },
    ]);

    expect(conflict?.tenant.name).toBe("한맥기술");
  });

  it("ignores the current tenant when checking domain conflicts", () => {
    const conflict = findDomainConflict(
      "hanmaceng.co.kr",
      [
        {
          id: "tenant-1",
          name: "한맥기술",
          slug: "hanmac",
          type: "COMPANY",
          description: "",
          status: "active",
          domains: ["hanmaceng.co.kr"],
          memberCount: 0,
          createdAt: "",
          updatedAt: "",
        },
      ],
      "tenant-1",
    );

    expect(conflict).toBeNull();
  });

  it("formats a duplicate domain message with tenant context", () => {
    expect(
      formatDomainConflictMessage({
        domain: "samaneng.com",
        tenantName: "한맥가족",
      }),
    ).toBe(
      "samaneng.com 도메인은 한맥가족 테넌트에 이미 설정되어 있습니다. 그래도 현재 테넌트에도 추가하시겠습니까?",
    );
  });
});
