import { del, get, postForm, put } from "./api.js";

export const modelManagementApi = {
  list(params = {}) {
    return get("/api/model-management/list", params).then((res) => {
      if (res?.code === 200) return res?.data || [];
      throw new Error(res?.message || "获取模型列表失败");
    });
  },

  upload({ file, modelName, modelType }) {
    const formData = new FormData();
    formData.append("file", file);
    formData.append("modelName", modelName);
    formData.append("modelType", modelType);
    return postForm("/api/model-management/upload", formData).then((res) => {
      if (res?.code === 200) return res?.data;
      throw new Error(res?.message || "上传模型失败");
    });
  },

  switchCurrent(modelId) {
    return put(`/api/model-management/${modelId}/switch`, {}).then((res) => {
      if (res?.code === 200) return res?.data;
      throw new Error(res?.message || "设置当前模型失败");
    });
  },

  update(modelId, { modelName, modelType }) {
    return put("/api/model-management", { modelId, modelName, modelType }).then((res) => {
      if (res?.code === 200) return res?.data;
      throw new Error(res?.message || "更新模型失败");
    });
  },

  remove(modelIds) {
    return del(`/api/model-management/${modelIds}`).then((res) => {
      if (res?.code === 200) return res?.data;
      throw new Error(res?.message || "删除模型失败");
    });
  },
};
