import { bluebookAiAgent } from "@/xiaolanbenlib/api/index";
import Taro from "@tarojs/taro";
import request from "@/xiaolanbenlib/module/axios.js";
import type { TKnowledgeItem, TQAListItem, TKnowledgeStreamResponseData, TKnowledgeDetail } from "@/types/knowledge";
import { getSimpleHeader } from "@/xiaolanbenlib/module/axios.js";
import { isSuccess } from "@/utils";
import { EKnowlegeTypes } from "@/consts/enum";


// 上传知识库文件
export const uploadKnowledgeFile = (data: 
  {
    tmpPath: string,
    fileName: string
  }
):Promise<boolean> => {
  return new Promise((resolve, reject)=> {
    Taro.uploadFile({
      url: `${bluebookAiAgent}api/v1/my/knowledge/upload`,
      filePath: data.tmpPath,
      name: data.fileName,
      formData: {
        file: data.fileName,
      },
      header: {
        ...getSimpleHeader(),
      },
      success(res) {
        console.log(res)
        resolve(isSuccess(res.statusCode))
      },
      fail(err) {
        console.log(err);
        reject(false);
      },
    });
  })
  
}


//我的知识库--列表
export type TKnowledgeListResponse = {
  "data": TKnowledgeItem[],
  "nextId": string,
  "totalCount": number
}


export const getKnowledgeList = ({startId, pageSize = 1, types = []}: {
  startId?: string;
  types?: EKnowlegeTypes[]
  pageSize: number;
}) => {
  return request.get<TKnowledgeListResponse>(
    `${bluebookAiAgent}api/v1/my/knowledge/list`,
    { params: {startId, pageSize, types: types.join(',')} }
  );
};


// 将指定的1个知识库共享至多个企业
export const shareToEnts = (knowledgeId: string|number, entIds: (string|number)[]) => {
  return request.post<{
    failEntIds: number[], // (Array[integer], optional): 失败的entId列表 ,
    successEntIds: number[] // 成功分享的entId列表
  }>(`${bluebookAiAgent}api/v1/my/knowledge/${knowledgeId}/share2Ents?entIds=${entIds.join(',')}`)
}
// 共享我的知识库至企业
export const shareToEnt = (data: 
  {
    "knowledgeIds": (number|string)[],
    "toEntId": string|number
  }
) => {
  return request.post(`${bluebookAiAgent}api/v1/my/knowledge/shareToEnt`, data)
}

// 获取指定的知识库已共享的企业列表，响应体为{'entIds':[]}
export const getSharedEnts = (knowledgeId: string|number) => {
  return request.get<{entIds: string[]}>(`${bluebookAiAgent}api/v1/my/knowledge/${knowledgeId}/sharedEnts`)
}



// 我的知识库--信息流

export type TKnowLedgeStreamResponse = {
  "data": TKnowledgeStreamResponseData[],
  "nextId": string,
  "totalCount": number
}
export const getMyKnowledgeStream = (data: {startId?: string, pageSize:number} ) => {
  return request.get<TKnowLedgeStreamResponse>(`${bluebookAiAgent}api/v1/my/knowledge/stream`, {params: data})
}



// 获取指定知识库项的详情信息
export const getMyKnowledgeDetail = (knowledgeId: number) => {
  return request.get<TKnowledgeDetail>(`${bluebookAiAgent}api/v1/my/knowledge/${knowledgeId}`)
}

// 是否启用文档精准问答
export const updateExactAnswer = (knowledgeId: number, enable: boolean) => {
  return request.put(`${bluebookAiAgent}api/v1/my/knowledge/${knowledgeId}/exactAnswer?enable=${enable}`)
}

// 删除知识库 信息流记录；流删除会同步删除相关的记录项
export const deleteMyKnowledgeStream = (streamId: string|number) => {
  return request.delete(`${bluebookAiAgent}api/v1/my/knowledge/stream/${streamId}`,)
}
// 删除个人知识库 记录项
export const deleteMyKnowledge = (knowledgeId: string|number) => {
  return request.delete(`${bluebookAiAgent}api/v1/my/knowledge/${knowledgeId}`,)
}
// 删除指定知识库的 QA 项
export const deleteKnowledgeQa = (knowledgeId: string|number, qaId: number|string) => {
  return request.delete(`${bluebookAiAgent}api/v1/my/knowledge/${knowledgeId}/${qaId}`,)
}

export const getKnowledgeQa = (knowledgeId: number|string, qaId: number|string) => {
  return request.get<TQAListItem>(`${bluebookAiAgent}api/v1/my/knowledge/${knowledgeId}/${qaId}`,)
}

// 更新指定的知识库 QA 项
export const updateKnowledgeQa = (knowledgeId: number|string, qaId: number|string, qa: TQAListItem&{qaId: string|number}) => {
  return request.put(`${bluebookAiAgent}api/v1/my/knowledge/${knowledgeId}/${qaId}`, qa)
}



// 企业知识库--列表
export const getEntKnowledgeList = ({startId, entId, pageSize, types = []}: {
  entId: string|number;
  startId?: string;
  types?: EKnowlegeTypes[]
  pageSize: number;
}) => {
  return request.get<TKnowledgeListResponse>(
    `${bluebookAiAgent}api/v1/ent/knowledge/list`,
    { params: {startId, entId, pageSize, types: types.join(',')}}
  );
};
// 获取指定知识库项的详情信息
export const getEntKnowledgeDetail = (knowledgeId: string|number, entId: string|number) => {
  return request.get(`${bluebookAiAgent}api/v1/ent/knowledge/${knowledgeId}?entId=${entId}`)
}

// 企业知识库--信息流
export const getEntKnowledgeStream = (data: {entId: string|number, startId?: string, pageSize:number} ) => {
  return request.get<TKnowLedgeStreamResponse>(`${bluebookAiAgent}api/v1/ent/knowledge/stream`, {params: data})
}


export const getEntKnowledgeQa = (knowledgeId: number|string, qaId: number|string) => {
  return request.get<TQAListItem>(`${bluebookAiAgent}api/v1/ent/knowledge/${knowledgeId}/${qaId}`)
}


