import { post, get, del, download } from '@/request/writingHttp'
import { fetchWithAbort } from '@/request/fetchWithAbort';
import useStore from '@/stores'

const { docs } = useStore();


/**
 * 2.1 获取模版
 * @returns
 * @param type 模版类型， 0 - 法定类， 1 - 事物类， 2 - 自定义
 */
export const getTemplate: (type: number) => Promise<any> = (type) => {
  return get(`/v1/template?official_document_type=${type}`, undefined)
}


/**
 * 2.2 获取模版字段数据
 * @returns
 * @param template_id
 */
export const getTemplateById: (template_id: number) => Promise<any> = (template_id) => {
  return get(`/v1/template/${template_id}`, undefined)
}


interface CreateDocumentData {
  template_type: number
  template_id: string
}

/**
 * 2.3 创建新文档
 * @returns
 * @param data
 */
export const createDocument: (data: {
  template_type: 0 | 1
  template_id: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
}) => Promise<any> = (data) => {
  return post(`/v1/document`, data)
}


/**
 * 2.4 获取文档基本信息
 * @returns
 */
export const getBasicInfo: () => Promise<any> = () => {
  return get(`/v1/document/${docs.document_id}/basic_info`)
}


/**
 * 2.5 提交基本信息字段数据
 * @returns
 * @param data
 */
export const saveBasicInfo: (data: any) => Promise<any> = (data) => {
  return post(`/v1/document/${docs.document_id}/basic_info`, data)
}



/**
 * 2.6 获取大纲
 * @returns
 */
export const getOutline: () => Promise<any> = () => {
  return get(`/v1/document/${docs.document_id}/outline`)
}


/**
 * 2.7 编辑大纲
 * @returns
 * @param data
 */
export const saveOutline: ( data: any) => Promise<any> = (data) => {
  return post(`/v1/document/${docs.document_id}/outline/${docs.outlineId}`, data)
}

interface SaveReferenceData {
  file: File,
  file_name: string
  file_type: 0 | 1 // 0 参考素材 1 数据参考
}

/**
 * 2.8 提交参考文献
 * @returns
 * @param data
 */
export const saveReference: (formData: FormData) => Promise<any> = (data) => {
  return post(`/v1/document/${docs.document_id}/reference`, data)
}


/**
 * 2.9 查看参考文献
 * @returns
 * @param reference_id
 */
export const delReference: (reference_id: string) => Promise<any> = (reference_id) => {
  return del(`/v1/document/reference/${reference_id}`)
}

interface actionDocumentData {
  // 0 - start 开始生成, 1- rewrite，重写, 2 - delete 删除, 3- approve 采纳
  action: 0 | 1 | 2 | 3,
}

/**
 * 2.10 文档操作
 * 生成公文正文, SSE
 * @returns
 * @param data
 */
export const actionDocument: (data: actionDocumentData) => Promise<any> = (data) => {
  return fetchWithAbort(`/ai-doc/api/v1/document/${docs.document_id}/content`, { ...data, model_id: docs.modelId })
}


/**
 * 2.11 获取文档列表
 * @returns
 * @param params
 */
export const getDocumentList: (params: any) => Promise<any> = (params) => {
  return get(`/v1/document`, params)
}


/**
 * 2.12 删除文档
 * @returns
 * @param document_ids
 */
export const deleteDocuments: (document_ids: any) => Promise<any> = (document_ids) => {
  return del(`/v1/document`, undefined, { document_ids })
}



/**
 * 2.13 获取文档
 * @returns
 * @param document_id
 */
export const getDocumentItem: (document_id: string) => Promise<any> = (document_id) => {
  return get(`/v1/document/${document_id}`, undefined)
}



/**
 * 2.14 编辑文档
 * @returns
 * @param data
 */
export const saveDocumentItem: (data: any) => Promise<any> = (data) => {
  return post(`/v1/document/${docs.document_id}`, data)
}

interface CreatePolishData {
  content: string
  action: 0 | 1 | 2 | 3// int, 0 - polish 润色，1 - extend 扩写
  preferences?: {
  
  }
}
/**
 * 2.15 ai润色
 * @returns
 * @param data
 */
export const createPolish: (data: CreatePolishData) => Promise<any> = (data) => {
  return fetchWithAbort(`/ai-doc/api/v1/document/polish`, { ...data, model_id: docs.modelId })
}

/**
 * 2.15 ai润色
 * @returns
 */
export const downloadDocument: () => Promise<any> = () => {
  return download(`/v1/document/${docs.document_id}/download`, 'get')
}


/**
 *
 * AI助手对话, SSE
 * @returns
 * @param data
 */
export const aiChat: (data: any) => Promise<any> = (data) => {
  return fetchWithAbort(`/ai-doc/api/v1/chat`, { ...data, model_id: docs.modelId })
}


/**
 *
 * 获取所有模型列表
 * @returns
 */
export const getAllModel: () => Promise<any> = () => {
  return get(`/v1/llm`)
}


/**
 *
 * 根据UUID获取特定LLM模型的配置详情
 * @returns
 */
export const getModel: (llm_uuid: string) => Promise<any> = (llm_uuid) => {
  return get(`/v1/llm/${llm_uuid}`)
}


/**
 *
 * 设置ai相关调用llm信息
 * @returns
 */
export const setModel: (id: string) => Promise<any> = (id) => {
  return post(`/v1/llm`, { id })
}
