import type { BulkUserItem, TenantSummary } from "../../../lib/adminApi";
import { applyGeneralPlanningOfficePriority } from "./generalPlanningOfficePriority";

function tenant(
  id: string,
  name: string,
  slug: string,
  parentId?: string,
): TenantSummary {
  return {
    id,
    type: "COMPANY",
    name,
    slug,
    description: "",
    status: "active",
    parentId,
    memberCount: 0,
    createdAt: "",
    updatedAt: "",
  };
}

describe("applyGeneralPlanningOfficePriority", () => {
  it("promotes the general planning office appointment and preserves string employee IDs", () => {
    const user: BulkUserItem = {
      email: "dual@test.com",
      name: "Dual User",
      tenantSlug: "hanmac-tech",
      department: "개발팀",
      grade: "책임",
      position: "팀장",
      jobTitle: "Backend",
      metadata: {
        employee_id: "EMP001",
      },
      additionalAppointments: [
        {
          tenantSlug: "planning-team",
          tenantName: "경영기획팀",
          department: "센터",
          grade: "수석",
          jobTitle: "Architecture",
          metadata: {
            employee_id: "EMP002",
          },
        },
      ],
    };

    const result = applyGeneralPlanningOfficePriority(user, [
      tenant("gpo", "총괄기획실", "gpo"),
      tenant("planning", "경영기획팀", "planning-team", "gpo"),
      tenant("tech", "한맥기술", "hanmac-tech"),
    ]);

    expect(result.tenantSlug).toBe("planning-team");
    expect(result.department).toBe("센터");
    expect(result.grade).toBe("수석");
    expect(result.jobTitle).toBe("Architecture");
    expect(result.metadata.employee_id).toBe("EMP002");
    expect(result.additionalAppointments?.[0]).toMatchObject({
      tenantSlug: "hanmac-tech",
      department: "개발팀",
      grade: "책임",
      position: "팀장",
      jobTitle: "Backend",
      metadata: {
        employee_id: "EMP001",
      },
    });
  });

  it("does not write non-string employee IDs into string metadata", () => {
    const user: BulkUserItem = {
      email: "dual@test.com",
      name: "Dual User",
      tenantSlug: "hanmac-tech",
      metadata: {
        employee_id: "EMP001",
      },
      additionalAppointments: [
        {
          tenantSlug: "gpo",
          tenantName: "총괄기획실",
          metadata: {
            employee_id: 1002,
          },
        },
      ],
    };

    const result = applyGeneralPlanningOfficePriority(user, [
      tenant("gpo", "총괄기획실", "gpo"),
      tenant("tech", "한맥기술", "hanmac-tech"),
    ]);

    expect(result.tenantSlug).toBe("gpo");
    expect(result.metadata.employee_id).toBeUndefined();
    expect(result.additionalAppointments?.[0].metadata).toMatchObject({
      employee_id: "EMP001",
    });
  });

  it("uses GPDTDC as the Baron representative while keeping the first affiliation primary for WorksMobile", () => {
    const user: BulkUserItem = {
      email: "gpdtdc-dual@test.com",
      name: "GPDTDC Dual User",
      tenantSlug: "rnd-saman",
      department: "삼안기술연구소",
      grade: "책임",
      metadata: {
        employee_id: "SAMAN001",
      },
      additionalAppointments: [
        {
          tenantSlug: "tdc",
          tenantName: "기술개발센터",
          grade: "책임연구원",
          metadata: {
            employee_id: "B24051",
          },
        },
      ],
    };

    const result = applyGeneralPlanningOfficePriority(user, [
      tenant("family", "한맥가족사", "hanmac-family"),
      tenant("gpdtdc", "총괄기획&기술개발센터", "gpdtdc", "family"),
      tenant("tdc", "기술개발센터", "tdc", "gpdtdc"),
      tenant("saman", "삼안", "rnd-saman"),
    ]);

    expect(result.tenantSlug).toBe("gpdtdc");
    expect(result.tenantImport).toMatchObject({
      slug: "gpdtdc",
      name: "총괄기획&기술개발센터",
    });
    expect(result.metadata.employee_id).toBe("SAMAN001");
    expect(result.additionalAppointments).toEqual([
      expect.objectContaining({
        tenantSlug: "rnd-saman",
        isPrimary: true,
        department: "삼안기술연구소",
        grade: "책임",
        metadata: {
          employee_id: "SAMAN001",
        },
      }),
      expect.objectContaining({
        tenantSlug: "tdc",
        isPrimary: false,
        grade: "책임연구원",
        metadata: {
          employee_id: "B24051",
        },
      }),
    ]);
  });
});
