import type { TenantSummary, UserSummary } from "../../lib/adminApi";

export type OrgPickerMode = "single" | "multiple";
export type OrgPickerSelectableType = "tenant" | "user" | "both";
export type OrgPickerObjectType = "tenant" | "user";

export type OrgPickerSelection = {
  type: OrgPickerObjectType;
  id: string;
  name: string;
};

export type OrgPickerResult = {
  mode: OrgPickerMode;
  selections: OrgPickerSelection[];
};

export type OrgPickerEmbedOptions = {
  includeInternal: boolean;
  mode: OrgPickerMode;
  select: OrgPickerSelectableType;
  includeDescendants: boolean;
  showDescendantToggle: boolean;
  tenantId: string;
  width: number;
  height: number;
};

export type OrgPickerTreeNode = {
  type: OrgPickerObjectType;
  id: string;
  name: string;
  parentId: string | null;
  tenant?: TenantSummary;
  user?: UserSummary;
  children: OrgPickerTreeNode[];
};

export function nodeKey(node: Pick<OrgPickerTreeNode, "type" | "id">) {
  return `${node.type}:${node.id}`;
}

export function selectionKey(selection: OrgPickerSelection) {
  return `${selection.type}:${selection.id}`;
}

export function parseOrgPickerMode(value: string | null): OrgPickerMode {
  return value === "multiple" ? "multiple" : "single";
}

export function parseOrgPickerSelectableType(
  value: string | null,
): OrgPickerSelectableType {
  if (value === "tenant" || value === "user") return value;
  return "both";
}

function parseEmbedDimension(value: string | null, fallback: number) {
  const parsed = Number.parseInt(value ?? "", 10);
  if (!Number.isFinite(parsed)) return fallback;
  return Math.min(1600, Math.max(240, parsed));
}

export function parseOrgPickerEmbedOptions(search: string) {
  const params = new URLSearchParams(search);
  return {
    mode:
      params.get("mode") === "single"
        ? ("single" as const)
        : ("multiple" as const),
    select: parseOrgPickerSelectableType(params.get("select")),
    includeInternal: params.get("includeInternal") === "true",
    includeDescendants: params.get("includeDescendants") !== "false",
    showDescendantToggle: params.get("showDescendantToggle") !== "false",
    tenantId:
      params.get("tenantSlug") ??
      params.get("tenantId") ??
      params.get("companyTenantId") ??
      "",
    width: parseEmbedDimension(params.get("width"), 400),
    height: parseEmbedDimension(params.get("height"), 600),
  };
}

export function buildOrgPickerEmbedSrc(options: OrgPickerEmbedOptions) {
  const params = new URLSearchParams({
    mode: options.mode,
    select: options.select,
    width: String(options.width),
    height: String(options.height),
  });
  if (options.includeInternal) {
    params.set("includeInternal", "true");
  }

  const tenantSlug = options.tenantId.trim();
  if (tenantSlug) {
    params.set("tenantSlug", tenantSlug);
  }

  if (options.mode === "multiple") {
    params.set("includeDescendants", String(options.includeDescendants));
    params.set("showDescendantToggle", String(options.showDescendantToggle));
  }

  return `/embed/picker?${params.toString()}`;
}
