import axios from "axios";

const API_BASE_URL = "http://127.0.0.1:8000/api";

// 创建axios实例
const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000,
  headers: {
    "Content-Type": "application/json"
  }
});

// 思维导图API
export const mindmapAPI = {
  // 创建思维导图
  createMindmap: (title = "思维导图", data = null) => {
    return api.post("/mindMaps", { title, data });
  },

  // 获取思维导图
  getMindmap: (id) => {
    return api.get(`/mindMaps/${id}`);
  },

  // 获取所有思维导图列表
  getAllMindmaps: () => {
    // 尝试不同的路径格式
    return api.get("/mindmaps");
  },

  // 添加节点
  addNodes: (mindMapId, nodes) => {
    return api.post("/mindMaps/addNodes", {
      mindMapId,
      nodes
    });
  },

  // 更新节点
  updateNode: (nodeId, updates) => {
    return api.patch("/mindMaps/updateNode", {
      id: nodeId,
      ...updates
    });
  },

  // 删除节点
  deleteNodes: (nodeIds) => {
    return api.delete("/mindMaps/deleteNodes", {
      data: { nodeIds }
    });
  }
};

export default api;
