import { describe, expect, it } from "vitest";
import type { TenantSummary } from "../../../lib/adminApi";
import {
  mergeTenantOrgConfig,
  ORG_UNIT_TYPE_OPTIONS,
  readTenantOrgConfig,
  removeTenantOrgConfig,
  shouldAllowHanmacOrgConfig,
} from "./orgConfig";

function tenant(
  id: string,
  type: string,
  name: string,
  slug: string,
  parentId?: string,
): TenantSummary {
  return {
    id,
    type,
    name,
    slug,
    description: "",
    status: "active",
    parentId,
    memberCount: 0,
    createdAt: "2026-05-11T00:00:00.000Z",
    updatedAt: "2026-05-11T00:00:00.000Z",
  };
}

describe("tenant org config", () => {
  it("allows org config only for hanmac-family descendants", () => {
    const family = tenant(
      "family",
      "COMPANY_GROUP",
      "한맥가족",
      "hanmac-family",
    );
    const saman = tenant("saman", "COMPANY", "삼안", "saman", "family");
    const team = tenant("team", "USER_GROUP", "기획팀", "planning", "saman");
    const outsider = tenant("outsider", "COMPANY", "외부", "outsider");
    const tenants = [family, saman, team, outsider];

    expect(shouldAllowHanmacOrgConfig(team, tenants)).toBe(true);
    expect(shouldAllowHanmacOrgConfig(family, tenants)).toBe(false);
    expect(shouldAllowHanmacOrgConfig(outsider, tenants)).toBe(false);
  });

  it("reads and writes tenant visibility and org unit type", () => {
    expect(
      readTenantOrgConfig({ visibility: "private", orgUnitType: "팀" }),
    ).toEqual({
      orgUnitType: "팀",
      visibility: "private",
      worksmobileExcluded: false,
    });
    expect(
      readTenantOrgConfig({ visibility: "internal", orgUnitType: "센터" }),
    ).toEqual({
      orgUnitType: "센터",
      visibility: "internal",
      worksmobileExcluded: false,
    });

    expect(
      mergeTenantOrgConfig(
        { userSchema: [], visibility: "private", orgUnitType: "팀" },
        {
          orgUnitType: "",
          visibility: "internal",
          worksmobileExcluded: false,
        },
      ),
    ).toEqual({
      userSchema: [],
      visibility: "internal",
      worksmobileExcluded: false,
    });
  });

  it("reads, writes, and removes the Worksmobile exclusion flag", () => {
    expect(readTenantOrgConfig({ worksmobileExcluded: true })).toMatchObject({
      worksmobileExcluded: true,
    });
    expect(readTenantOrgConfig({ worksmobileExcluded: "true" })).toMatchObject({
      worksmobileExcluded: true,
    });
    expect(
      mergeTenantOrgConfig(
        { userSchema: [], worksmobileExcluded: false },
        {
          orgUnitType: "팀",
          visibility: "private",
          worksmobileExcluded: true,
        },
      ),
    ).toEqual({
      userSchema: [],
      orgUnitType: "팀",
      visibility: "private",
      worksmobileExcluded: true,
    });
    expect(
      removeTenantOrgConfig({
        userSchema: [],
        orgUnitType: "팀",
        visibility: "private",
        worksmobileExcluded: true,
      }),
    ).toEqual({ userSchema: [] });
  });

  it("includes task-force and executive-direct org unit types", () => {
    expect(ORG_UNIT_TYPE_OPTIONS).toEqual(
      expect.arrayContaining(["TF", "TF팀", "임원직속"]),
    );
    expect(readTenantOrgConfig({ orgUnitType: "TF" }).orgUnitType).toBe("TF");
    expect(readTenantOrgConfig({ orgUnitType: "TF팀" }).orgUnitType).toBe(
      "TF팀",
    );
    expect(readTenantOrgConfig({ orgUnitType: "임원직속" }).orgUnitType).toBe(
      "임원직속",
    );
  });
});
