import request from '../utils/request'

// ============================== 图谱源 CRUD ==============================

export function getGraphSources() {
  return request.get('/kb/graphs')
}

export function getGraphSource(id) {
  return request.get(`/kb/graphs/${id}`)
}

export function createGraphSource(data) {
  return request.post('/kb/graphs', data)
}

export function updateGraphSource(id, data) {
  return request.put(`/kb/graphs/${id}`, data)
}

export function deleteGraphSource(id) {
  return request.delete(`/kb/graphs/${id}`)
}

/** 测试连接（不持久化结果，用于表单预检） */
export function testGraphSource(data) {
  return request.post('/kb/graphs/test', data)
}

/** 测试已存在的图谱源（持久化结果） */
export function testExistingGraphSource(id) {
  return request.post(`/kb/graphs/${id}/test`)
}

// ============================== Cypher 查询 ==============================

/**
 * 执行只读 Cypher 查询，返回 cytoscape elements
 * @param {number} graphId
 * @param {string} cypher
 * @returns {Promise<{data: {nodes: [], edges: [], truncated: boolean, durationMs: number}}>}
 */
export function executeGraphQuery(graphId, cypher) {
  return request.post(`/kb/graphs/${graphId}/query`, { cypher })
}

/** 列出图谱的所有 node labels */
export function getGraphLabels(graphId) {
  return request.get(`/kb/graphs/${graphId}/labels`)
}
