const { ManagementClient } = require('authing-node-sdk')
const managementClient  = new ManagementClient({
    accessKeyId: '6686bffb373d06911e24a969',
    accessKeySecret: "4e978331675938d1bc81fb109e67d59a",
    host: 'https://textbook.u2-dev.hep.com.cn', // 应用的认证地址
})


// const pgp = require('pg-promise')();
// const pgClient = () => {return pgp("postgresql://textbook:Edu2024textbask@oss-cn-beijing-internal.aliyuncs.com:5432/textbook");}

import { pgClient } from '../../db/pg-instance'
export function defineAuthingDepartSync(){
// function defineAuthingLogin(){
    Parse.Cloud.define("authingDepartSync", async (request) => {
        let result = await syncOrganizationsFromAuthing()
        return result
    });
}
  

/**
 * 同步全部组织架构 From Authing
 * @desc
 * https://docs.authing.cn/v3/reference/sdk/node/management/管理组织机构/list-organizations.html
 * @param {*} token 
 * @returns 
 */
async function syncOrganizationsFromAuthing(){
    // 通过用户的 id_token 初始化之后获取用户信息
    console.log("syncOrganizationsFromAuthing 2")
    
    console.log("managementClient")
    let orgList = []
    try{

        let result = await managementClient.listOrganizations({
            page: 1,
            limit: 50,
            withPost: true,
            withCustomData: true
        });
        if(result?.data?.list?.length){
            orgList = orgList.concat(result?.data?.list)
            let departList = await getAllChildDepartments(orgList[0])
            console.log(departList)
            console.log(departList?.length)
            let insertRes = await InsertAllDepartment(departList)
            console.log(insertRes)
            return {code:200,count:departList?.length,message:`成功导入${departList?.length}条`}
        }
        
    }catch(err){
        console.error(err)
    }
    return
    // console.log(user)

}
module.exports.syncOrganizationsFromAuthing = syncOrganizationsFromAuthing

async function InsertAllDepartment(departList){
    // {
        //     departmentId: '66868df17adf199d1c77875d',
    //     organizationCode: '1000000',
    //     name: '河北人民出版社有限责任公司',
    //     description: '',
    //     code: '30000095',
    //     hasChildren: false,
    //     isVirtualNode: false,
    //     i18n: { name: [Object] },
    //     customData: {},
    //     status: true
    //     createdAt: '2024-07-04T11:56:33.813Z',
    //     updatedAt: '2024-07-04T11:56:33.813Z',
    //   },
    let rowData = []
    departList.forEach(depart=>{
        let objectId = depart?.departmentId
        rowData.push([
            objectId,depart?.organizationCode,depart?.name,depart?.description,depart?.parent?.name,
            depart?.parent?.departmentId,depart?.code,depart?.createdAt || new Date(),depart?.updatedAt || new Date(),depart?.status])
    })
    let flattenedParams = rowData.flat();

    let syncDepartSQL = `
    INSERT INTO "Department" ("objectId", "organizationCode", "name", "description","branch","parent","code","createdAt","updatedAt","status")
    VALUES
        ${rowData.map((_, i) => `($${i * 10 + 1}, $${i * 10 + 2}, $${i * 10 + 3}, $${i * 10 + 4}, $${i * 10 + 5}, $${i * 10 + 6}, $${i * 10 + 7}, $${i * 10 + 8}, $${i * 10 + 9}, $${i * 10 + 10})`).join(',\n')}    
    ON CONFLICT ("objectId") DO UPDATE
    SET 
        "organizationCode" = EXCLUDED."organizationCode",
        "name" = EXCLUDED."name",
        "description" = EXCLUDED."description",
        "branch" = EXCLUDED."branch",
        "parent" = EXCLUDED."parent",
        "code" = EXCLUDED."code",
        "status" = EXCLUDED."status",
        "createdAt" = EXCLUDED."createdAt",
        "updatedAt" = EXCLUDED."updatedAt";
    `
    let result = await pgClient().any(syncDepartSQL,flattenedParams);
    return result
}

async function getAllChildDepartments(depart){
    if(!depart?.hasChildren) return []
    let departmentList = []

    let childDepartList = await getDepartmentOfOrg(depart?.organizationCode,depart?.departmentId)
    departmentList = departmentList.concat(childDepartList)
    // console.log("childDepartList",childDepartList)
    let pList = childDepartList.map(item=>getAllChildDepartments(item))
    let resList = await Promise.all(pList);
    resList.forEach((list,index)=>{
        list = list.map(item=>{item.parent = childDepartList[index];return item})
        departmentList = departmentList.concat(list)
    })
    return departmentList
}

async function getDepartmentOfOrg(organizationCode,departmentId){
    let result = await managementClient.listChildrenDepartments({
        // 替换组织 Code 和部门 ID
        organizationCode: organizationCode,
        departmentId: departmentId,
        departmentIdType: 'department_id',
        withCustomData: true
      });
      if(result?.data?.list?.length) return result?.data?.list
      return []
}

// const crypto = require('crypto');
// function generateObjectId(inputString) {
//     const hash = crypto.createHash('sha256').update(inputString).digest('hex');
//     const objectId = hash;
//     return objectId;
// }