import { describe, expect, it } from "vitest";
import {
  buildAuthenticatedOrgChartTenantPickerUrl,
  buildAuthenticatedOrgChartUrl,
  buildOrgChartTenantPickerUrl,
  filterNonHanmacFamilyTenants,
  isHanmacFamilyUser,
  parseOrgChartTenantSelection,
} from "./orgChartPicker";

describe("orgChartPicker", () => {
  it("builds the tenant picker embed URL from ORGFRONT_URL", () => {
    expect(buildOrgChartTenantPickerUrl("https://orgchart.example.com/")).toBe(
      "https://orgchart.example.com/embed/picker?mode=single&select=tenant&width=400&height=600",
    );
  });

  it("adds tenantId to the tenant picker URL when Hanmac family scope is known", () => {
    expect(
      buildOrgChartTenantPickerUrl("https://orgchart.example.com/", {
        tenantId: "hanmac-family-id",
      }),
    ).toBe(
      "https://orgchart.example.com/embed/picker?mode=single&select=tenant&width=400&height=600&tenantId=hanmac-family-id",
    );
  });

  it("wraps the picker URL with the org-chart auto login entry", () => {
    expect(
      buildAuthenticatedOrgChartTenantPickerUrl(
        "https://orgchart.example.com",
        {
          tenantId: "hanmac-family-id",
        },
      ),
    ).toBe(
      "https://orgchart.example.com/login?auto=1&returnTo=%2Fembed%2Fpicker%3Fmode%3Dsingle%26select%3Dtenant%26width%3D400%26height%3D600%26tenantId%3Dhanmac-family-id",
    );
  });

  it("builds the chart navigation URL through the org-chart auto login entry", () => {
    expect(buildAuthenticatedOrgChartUrl("https://orgchart.example.com/")).toBe(
      "https://orgchart.example.com/login?auto=1&returnTo=%2Fchart",
    );
  });

  it("parses the first tenant id and name from orgfront confirm messages", () => {
    expect(
      parseOrgChartTenantSelection({
        type: "orgfront:picker:confirm",
        payload: {
          mode: "single",
          selections: [
            {
              type: "tenant",
              id: "03dbe16b-e47b-4f72-927b-782807d67a35",
              name: "기술기획",
            },
          ],
        },
      }),
    ).toEqual({
      id: "03dbe16b-e47b-4f72-927b-782807d67a35",
      name: "기술기획",
    });
  });

  it("ignores non-tenant or malformed picker messages", () => {
    expect(
      parseOrgChartTenantSelection({
        type: "orgfront:picker:confirm",
        payload: {
          mode: "single",
          selections: [{ type: "user", id: "u-1", name: "User" }],
        },
      }),
    ).toBeNull();

    expect(parseOrgChartTenantSelection({ type: "other" })).toBeNull();
  });

  it("filters Hanmac family subtree and system tenants from non-family tenant choices", () => {
    const visibleTenants = filterNonHanmacFamilyTenants(
      [
        {
          id: "system-id",
          slug: "system",
          name: "System",
          type: "SYSTEM",
          parentId: undefined,
        },
        {
          id: "external-id",
          slug: "external",
          name: "External",
          type: "COMPANY",
          parentId: undefined,
        },
        {
          id: "hanmac-family-id",
          slug: "hanmac-family",
          name: "한맥가족",
          type: "COMPANY_GROUP",
          parentId: undefined,
        },
        {
          id: "hanmac-company-id",
          slug: "hanmac-company",
          name: "한맥기술",
          type: "COMPANY",
          parentId: "hanmac-family-id",
        },
        {
          id: "hanmac-team-id",
          slug: "hanmac-team",
          name: "한맥팀",
          type: "USER_GROUP",
          parentId: "hanmac-company-id",
        },
      ],
      "hanmac-family-id",
    );

    expect(visibleTenants.map((tenant) => tenant.slug)).toEqual(["external"]);
  });

  it("detects existing users as Hanmac family from tenant subtree without metadata flag", () => {
    const tenants = [
      {
        id: "external-id",
        slug: "external",
        name: "External",
        type: "COMPANY",
        parentId: undefined,
      },
      {
        id: "hanmac-family-id",
        slug: "hanmac-family",
        name: "한맥가족",
        type: "COMPANY_GROUP",
        parentId: undefined,
      },
      {
        id: "hanmac-company-id",
        slug: "hanmac-company",
        name: "한맥기술",
        type: "COMPANY",
        parentId: "hanmac-family-id",
      },
      {
        id: "hanmac-team-id",
        slug: "hanmac-team",
        name: "기술기획",
        type: "USER_GROUP",
        parentId: "hanmac-company-id",
      },
    ];

    expect(
      isHanmacFamilyUser(
        {
          companyCode: "external",
          tenant: tenants[0],
          joinedTenants: [tenants[3]],
          metadata: {},
        },
        tenants,
        "hanmac-family-id",
      ),
    ).toBe(true);
  });

  it("does not treat legacy hanmacFamily metadata as Hanmac family without tenant evidence", () => {
    const tenants = [
      {
        id: "hanmac-family-id",
        slug: "hanmac-family",
        name: "한맥가족",
        type: "COMPANY_GROUP",
        parentId: undefined,
      },
      {
        id: "external-id",
        slug: "external",
        name: "External",
        type: "COMPANY",
        parentId: undefined,
      },
    ];

    expect(
      isHanmacFamilyUser(
        {
          companyCode: "external",
          tenant: tenants[1],
          metadata: { hanmacFamily: true },
        },
        tenants,
        "hanmac-family-id",
      ),
    ).toBe(false);
  });

  it("does not treat userType metadata as Hanmac family without tenant evidence", () => {
    const tenants = [
      {
        id: "hanmac-family-id",
        slug: "hanmac-family",
        name: "한맥가족",
        type: "COMPANY_GROUP",
        parentId: undefined,
      },
      {
        id: "external-id",
        slug: "external",
        name: "External",
        type: "COMPANY",
        parentId: undefined,
      },
    ];

    expect(
      isHanmacFamilyUser(
        {
          companyCode: "external",
          tenant: tenants[1],
          metadata: { userType: "hanmac" },
        },
        tenants,
        "hanmac-family-id",
      ),
    ).toBe(false);
  });
});
