import { request } from '@/utils/request'

/* 知识库部分开始 */

/**
 * @query trainStatus 训练状态
 * @query search 搜索值
 * @returns 获取知识库列表
 */
export function fetchGetKnowledgeList<T>(trainStatus: string, search: string, payload: object) {
  return request.post<T>(`/knowledgeRest/getKnowledgeList.json?trainStatus=${trainStatus}&search=${search}`, payload)
}

/**
 * @param payload { decs: string, knowledgeName: string }
 * @returns 新建知识库
 */
export function fetchCreateKnowledge<T>(payload: object) {
  return request.post<T>('/knowledgeRest/createKnowledge.json', payload)
}

/**
 * @param payload { id：number, decs: string, knowledgeName: string }
 * @returns 更新知识库
 */
export function fetchUpdateKnowledgeInfo<T>(payload: object) {
  return request.post<T>('/knowledgeRest/updateKnowledgeInfo.json', payload)
}

/**
 * @query knowledgeInfoId 知识库Id
 * @returns 通过Id获取知识库详情
 */
export function fetchGetKnowledgeDetail<T>(knowledgeInfoId: number) {
  return request.post<T>(`/knowledgeRest/getKnowledgeDetail.json?knowledgeInfoId=${knowledgeInfoId}`)
}

/**
 * @query knowledgeInfoId 知识库Id
 * @query isOpen 是否开启 Y | N
 * @returns 开关知识库
 */
export function fetchEnableKnowledgeInfo(knowledgeInfoId: number, isOpen: string) {
  return request.post(`/knowledgeRest/enableKnowledgeInfo.json?knowledgeInfoId=${knowledgeInfoId}&isOpen=${isOpen}`)
}

/**
 * @query knowledgeInfoId 知识库Id
 * @returns 批量删除知识库
 */
export function fetchDelKnowledgeById<T>(knowledgeInfoId: number) {
  return request.post<T>(`/knowledgeRest/deleteKnowledgeInfo.json?knowledgeInfoId=${knowledgeInfoId}`)
}

/**
 * @query formData 上传文档内容
 * @returns 上传知识库文档
 */
export function fetchUploadKnowledgeDocument<T>(formData: FormData) {
  return request.post<T>('/knowledgeRest/uploadDocument.json', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    timeout: 300000,
  })
}

/**
 * @params { knowledgeInfoId: number, kdIds: number[], segmentationConfig: SegmentationConfigInterface }
 * @returns 训练知识库
 */
export function fetchTrainKnowledge<T>(payload: object) {
  return request.post<T>('/knowledgeRest/trainKnowledge.json', payload)
}

/* 知识库部分结束 */

/* 知识库文档部分开始 */

/**
 * @query knowledgeInfoId 知识库Id
 * @query search 搜索值
 * @returns 获取知识库文档列表
 */
export function fetchGetKnowledgeDocumentList<T>(knowledgeInfoId: number, search: string) {
  return request.post<T>(`/knowledgeRest/searchDocuments.json?knowledgeInfoId=${knowledgeInfoId}&search=${search}`)
}

/**
 * @query knowledgeInfoId 知识库Id
 * @params kdIds 知识库文档Id
 * @returns 批量删除知识库文档
 */
export function fetchBatchDelKnowledgeDocument(knowledgeInfoId: number, payload: number[]) {
  return request.post(`/knowledgeRest/batchDelDocument.json?knowledgeInfoId=${knowledgeInfoId}`, payload)
}

/**
 * @query knowledgeInfoId 知识库Id
 * @query kdId 知识库文档Id
 * @returns 删除知识库文档
 */
export function fetchDelKnowledgeDocument(knowledgeInfoId: number, kdId: number) {
  return request.post(`/knowledgeRest/delDocument.json?knowledgeInfoId=${knowledgeInfoId}&kdId=${kdId}`)
}

/**
 * @params kdIds 知识库文档Id
 * @returns 根据kdIds获取知识库文档列表
 */
export function fetchGetKnowledgeDocumentListByKdIds<T>(kdIds: number[]) {
  return request.post<T>(`/knowledgeRest/getListByKdIds.json`, kdIds)
}

/* 知识库文档片段开始 */

/**
 * @query query 模糊搜索
 * @query kdId 知识库文档Id
 * @params payload { page: number, pageSize: number }
 * @returns 获取知识库分片列表
 */
export function fetchGetKnowledgeChunkList<T>(query: string, kdId: number, payload: object) {
  return request.post<T>(`/knowledgeRest/getChunks.json?query=${query}&kdIds=${kdId}`, payload)
}

/**
 * @params payload { kdId: number, chunkContent: string, chunkSort: number }
 * @returns 新增知识库分片
 */
export function fetchAddKnowledgeChunk<T>(payload: { kdId: number; chunkContent: string; chunkSort: number }) {
  return request.post<T>('/knowledgeRest/addKnowledgeChunk.json', payload)
}

/**
 * @params payload { kdId: number, chunkContent: string, chunkRelationId: string }
 * @returns 更新知识库分片
 */
export function fetchUpdateKnowledgeChunk<T>(payload: { kdId: number; chunkContent: string; chunkRelationId: string }) {
  return request.post<T>('/knowledgeRest/updateKnowledgeChunkDoc.json', payload)
}

/**
 * @params payload { kdId: number, chunkRelationId: string }
 * @returns 删除知识库分片
 */
export function fetchDeleteKnowledgeChunk<T>(payload: { kdId: number; chunkRelationId: string }) {
  return request.post<T>('/knowledgeRest/deleteKnowledgeChunk.json', payload)
}

/**
 * @params payload { kdId: number, chunkRelationId: string, isOpen: 'Y' | 'N' }
 * @returns 开关知识库分片
 */
export function fetchOpenKnowledgeChunk<T>(payload: { kdId: number; chunkRelationId: string; isOpen: 'Y' | 'N' }) {
  return request.post<T>('/knowledgeRest/openKnowledgeChunk.json', payload)
}
/* 知识库文档部分结束 */
