import { download, get, post, upload } from '@/api/request';

const env = import.meta.env;
const PREFIX = env.VITE_API_PREFIX_DATA;
const WS_PREFIX = env.VITE_API_WS_BASE_URL;

// 获取模型列表
export const getModelTrainingListApi = (params: any) => {
  return post(`${PREFIX}modelTraining/getModelList`, params);
};
// 导出模型列表
export const exportTrainingListApi = (params: any, fileName: string) => {
  return download(`${PREFIX}modelTraining/exportModelList`, params, fileName);
};
// 获取模型详情
export const getModelDetailApi = (params: any) => {
  return get(`${PREFIX}modelTraining/getModelDetail`, params);
};
// 新增训练模型
export const addModelApi = (params: any) => {
  return post(`${PREFIX}modelTraining/addModel`, params);
};
// 编辑训练模型
export const updateModelApi = (params: any) => {
  return post(`${PREFIX}modelTraining/updateModel`, params);
};
// 删除模型
export const delModelApi = (params: any) => {
  return get(`${PREFIX}modelTraining/deleteModel`, params);
};
// 调用python脚本处理导入数据
export const handleLoadDataApi = (params: any) => {
  return upload(`${PREFIX}modelTraining/handleLoadData`, params);
};
// 获取python脚本训练数据处理结果
export const getHandleLoadDataResultApi = (params: any) => {
  return get(`${PREFIX}modelTraining/getHandleLoadDataResult`, params);
};
// 设置训练数据输入输出特征列
export const setTrainingDataInputOutputColumnApi = (params: any) => {
  return post(`${PREFIX}modelTraining/setTrainingDataInPutOutPutColumn`, params);
};
// 获取训练数据输入输出特征列
export const getTrainingDataInputOutputColumnApi = (params: any) => {
  return get(`${PREFIX}modelTraining/getTrainingDataInPutOutPutColumn`, params);
};
// 算法参数设置并提交训练
export const submitTrainingApi = (params: any) => {
  return post(`${PREFIX}modelTraining/submitTraining`, params);
};
// 获取算法参数设置详情
export const getAlgorithmParamApi = (params: any) => {
  return get(`${PREFIX}modelTraining/getAlgorithmParam`, params);
};
// 获取训练曲线和训练日志
export const getTrainingResultApi = (params: any) => {
  return get(`${PREFIX}modelTraining/getTrainingResult`, params);
};
// 停止模型训练
export const stopTrainingApi = (params: any) => {
  return get(`${PREFIX}modelTraining/stopTraining`, params);
};
// 数据预测
export const startPredictApi = (params: any) => {
  return post(`${PREFIX}modelTraining/startPredict`, params);
};
// 获取历史模型预测结果
export const getModelPredictResultApi = (params: any) => {
  return get(`${PREFIX}modelTraining/getModelPredictResult`, params);
};

// websocket 连接地址
export const getModelTrainingWebSocketUrl = (userId: string, modelId: string) => {
  try {
    return `${WS_PREFIX}/data/ws/data/modelTraining?userId=${encodeURIComponent(
      userId
    )}&modelId=${encodeURIComponent(modelId)}`;
  } catch (e) {
    console.error('获取websocket地址失败', e);
    return '';
  }
};
