import { PosMaster } from "../entities/PosMaster";

export interface PosMasterFormatted {
  posmasterId: string;
  posNo: string;
  orgTreeId: string;
  orgLevel: number;
  fullNameCurrentHolder: string | null;
}

export interface OrgTreeNode {
  orgTreeId: string;
  orgLevel: number;
  orgName: string;
  orgTreeName: string;
  orgTreeShortName: string;
  orgTreeCode: string;
  orgCode: string;
  orgRootName: string;
  labelName: string;
  posMaster: PosMasterFormatted[];
  children?: OrgTreeNode[];
}

export function formatPosMaster(
  posMaster: PosMaster,
  orgShortName: string,
  orgTreeId: string,
  orgLevel: number,
): PosMasterFormatted {
  return {
    posmasterId: posMaster.id,
    posNo: `${orgShortName} ${posMaster.posMasterNo}`,
    orgTreeId,
    orgLevel,
    fullNameCurrentHolder: posMaster.current_holder
      ? `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`
      : null,
  };
}

export function generateLabelName(
  nodeName: string,
  nodeCode: string,
  nodeShortName: string,
  rootName: string,
  rootCode: string,
  rootShortName: string,
  parentNames?: string[],
  parentCodes?: string[],
  parentShortNames?: string[],
): string {
  const parts = [nodeName, " ", rootCode, nodeCode, " ", nodeShortName];

  if (parentNames) {
    for (let i = 0; i < parentNames.length; i++) {
      parts.push("/", parentNames[i], " ", rootCode, parentCodes![i], " ", parentShortNames![i]);
    }
  }

  parts.push("/", rootName, " ", rootCode, "00", " ", rootShortName);
  return parts.join("");
}

export function filterPosMasters(
  posMasters: PosMaster[],
  childLevelIdKey: keyof PosMaster,
): PosMaster[] {
  return posMasters.filter((x) => x[childLevelIdKey] == null && x.isDirector === true);
}

/**
 * สร้าง orgShortName จาก posMaster (ต้อง load org relations มาก่อน)
 */
export function getOrgShortName(posMaster: PosMaster): string {
  if (posMaster.orgChild1Id === null) {
    return posMaster.orgRoot?.orgRootShortName ?? "";
  } else if (posMaster.orgChild2Id === null) {
    return posMaster.orgChild1?.orgChild1ShortName ?? "";
  } else if (posMaster.orgChild3Id === null) {
    return posMaster.orgChild2?.orgChild2ShortName ?? "";
  } else if (posMaster.orgChild4Id === null) {
    return posMaster.orgChild3?.orgChild3ShortName ?? "";
  } else {
    return posMaster.orgChild4?.orgChild4ShortName ?? "";
  }
}

/**
 * สร้างชื่อสังกัดเต็ม จาก posMaster (join ด้วย \n)
 */
export function getOrgFullName(posMaster: PosMaster): string {
  const parts = [
    posMaster.orgChild4?.orgChild4Name,
    posMaster.orgChild3?.orgChild3Name,
    posMaster.orgChild2?.orgChild2Name,
    posMaster.orgChild1?.orgChild1Name,
    posMaster.orgRoot?.orgRootName,
  ];
  return parts.filter((part) => part !== undefined && part !== null).join("\n");
}

/**
 * สร้างเลขที่ตำแหน่ง เช่น "กทม. กบ. 1234 ช"
 */
export function getPosMasterNo(posMaster: PosMaster): string {
  const orgShortName = getOrgShortName(posMaster);
  const parts = [
    posMaster.posMasterNoPrefix,
    posMaster.posMasterNo,
    posMaster.posMasterNoSuffix,
  ].filter((part) => part !== null && part !== undefined);
  return `${orgShortName} ${parts.join(' ')}`;
}
