import { describe, expect, it } from "vitest";
import type { UserSummary } from "../../lib/adminApi";
import { getOrgChartUserDisplayName, getUserOrgProfile } from "./userDisplay";

function user(overrides: Partial<UserSummary>): UserSummary {
  return {
    id: "user-1",
    email: "user@example.com",
    name: "홍길동",
    role: "user",
    status: "active",
    createdAt: "",
    updatedAt: "",
    ...overrides,
  };
}

describe("getOrgChartUserDisplayName", () => {
  it("renders name with grade and without job details", () => {
    expect(
      getOrgChartUserDisplayName(
        user({
          grade: "수석",
          position: "팀장",
          jobTitle: "구조",
        }),
      ),
    ).toBe("홍길동 수석");
  });

  it("uses tenant appointment grade before the user grade", () => {
    expect(
      getOrgChartUserDisplayName(
        user({
          grade: "책임",
          metadata: {
            additionalAppointments: [
              {
                tenantSlug: "hanmac",
                grade: "수석",
                position: "센터장",
              },
            ],
          },
        }),
        { id: "tenant-1", slug: "hanmac" },
      ),
    ).toBe("홍길동 수석");
  });

  it("uses short grade aliases in the display name", () => {
    expect(
      getOrgChartUserDisplayName(
        user({
          grade: "책임연구원",
          jobTitle: "구조",
        }),
      ),
    ).toBe("홍길동 책임");
  });

  it("does not add leader text to the display name", () => {
    expect(
      getOrgChartUserDisplayName(
        user({
          grade: "책임",
          metadata: {
            additionalAppointments: [
              {
                tenantSlug: "hanmac",
                isOwner: true,
                grade: "수석",
                position: "센터장",
              },
            ],
          },
        }),
        { id: "tenant-1", slug: "hanmac" },
      ),
    ).toBe("홍길동 수석");
  });

  it("does not leak an owner appointment flag into another tenant display", () => {
    expect(
      getOrgChartUserDisplayName(
        user({
          grade: "책임",
          position: "팀원",
          metadata: {
            additionalAppointments: [
              {
                tenantSlug: "hanmac",
                isOwner: true,
                grade: "수석",
                position: "센터장",
              },
            ],
          },
        }),
        { id: "tenant-2", slug: "baron" },
      ),
    ).toBe("홍길동 책임");
  });
});

describe("getUserOrgProfile", () => {
  it("marks owner, manager, and admin flags as highlighted profiles", () => {
    expect(
      getUserOrgProfile(
        user({
          metadata: {
            additionalAppointments: [
              {
                tenantSlug: "owner",
                isOwner: true,
              },
              {
                tenantSlug: "manager",
                isManager: true,
              },
              {
                tenantSlug: "admin",
                isAdmin: true,
              },
            ],
          },
        }),
        { id: "tenant-1", slug: "owner" },
      ).isHighlighted,
    ).toBe(true);
    expect(
      getUserOrgProfile(
        user({
          metadata: {
            additionalAppointments: [{ tenantSlug: "leader", isLeader: true }],
          },
        }),
        { id: "tenant-2", slug: "leader" },
      ).isHighlighted,
    ).toBe(false);
    expect(
      getUserOrgProfile(
        user({
          metadata: {
            additionalAppointments: [
              { tenantSlug: "manager", isManager: true },
            ],
          },
        }),
        { id: "tenant-2", slug: "manager" },
      ).isHighlighted,
    ).toBe(true);
    expect(
      getUserOrgProfile(
        user({
          metadata: {
            additionalAppointments: [{ tenantSlug: "admin", isAdmin: true }],
          },
        }),
        { id: "tenant-3", slug: "admin" },
      ).isHighlighted,
    ).toBe(true);
    expect(getUserOrgProfile(user({ grade: "책임" })).isHighlighted).toBe(
      false,
    );
  });
});
