import request from '../utils/request'

export function getSkillList(params = {}) {
  return request.get('/skills', { params })
}

export function uploadSkill(file, onProgress) {
  const formData = new FormData()
  formData.append('file', file)
  return request.post('/skills/upload', formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
    onUploadProgress: onProgress
  })
}

export function editSkill(folderName, data) {
  return request.post(`/skills/${encodeURIComponent(folderName)}/edit`, data)
}

export function deleteSkill(folderName) {
  return request.post(`/skills/${encodeURIComponent(folderName)}/delete`)
}

// ==================== 技能编辑相关 ====================

export function getSkillDetail(folderName) {
  return request.get(`/skills/${encodeURIComponent(folderName)}/detail`)
}

export function saveSkillMd(folderName, data) {
  return request.put(`/skills/${encodeURIComponent(folderName)}/skill-md`, data)
}

export function listSkillFiles(folderName) {
  return request.get(`/skills/${encodeURIComponent(folderName)}/files`)
}

export function readSkillFile(folderName, path) {
  return request.get(`/skills/${encodeURIComponent(folderName)}/files/content`, { params: { path } })
}

export function saveSkillFile(folderName, path, content) {
  return request.put(`/skills/${encodeURIComponent(folderName)}/files/content`, { path, content })
}

export function createSkillFile(folderName, path, type) {
  return request.post(`/skills/${encodeURIComponent(folderName)}/files`, { path, type })
}

export function deleteSkillFile(folderName, path) {
  return request.delete(`/skills/${encodeURIComponent(folderName)}/files`, { params: { path } })
}

// ==================== 版本管理 ====================

export function listVersions(folderName) {
  return request.get(`/skills/${encodeURIComponent(folderName)}/versions`)
}

export function getVersionContent(folderName, version) {
  return request.get(`/skills/${encodeURIComponent(folderName)}/versions/${version}/content`)
}

export function rollbackVersion(folderName, version) {
  return request.post(`/skills/${encodeURIComponent(folderName)}/versions/${version}/rollback`)
}

export function updateVersionMessage(folderName, version, message) {
  return request.put(`/skills/${encodeURIComponent(folderName)}/versions/${version}/message`, { message })
}

// ==================== 技能-分类关联 ====================

export function setSkillCategory(folderName, categoryId) {
  return request.put(`/skills/${encodeURIComponent(folderName)}/category`, { categoryId })
}

// ==================== 技能生成 ====================

/** 列出具备生成其他技能能力的技能 */
export function listGeneratorSkills() {
  return request.get('/skills/generators')
}

/** 启动一次技能生成任务，返回 runId */
export function startSkillGeneration(payload) {
  return request.post('/skills/generate', payload)
}

/** 查询生成状态（轻量轮询兜底） */
export function getSkillGenerationStatus(runId) {
  return request.get(`/skills/generate/${encodeURIComponent(runId)}/status`)
}

/**
 * 构造技能生成 SSE 流的 URL（供 EventSource 使用）。
 * 因 EventSource 不支持自定义 header，Last-Event-ID 通过 query 参数传递。
 */
export function buildSkillGenerationStreamUrl(runId, lastEventId) {
  const base = `/api/skills/generate/${encodeURIComponent(runId)}/stream`
  return lastEventId != null ? `${base}?lastEventId=${lastEventId}` : base
}

/**
 * 智能润色需求文本：调用 military-workflow-spec 技能做规格化。
 * 返回 { status, message, prompt }。
 *
 * 注：技能规格化由 Hermes Agent 同步执行，LLM 通常需要 30-90 秒，
 *     这里单独设置 180 秒超时，避免被 axios 默认 30 秒超时切断。
 */
export function refineSkillRequirement(userInput, refineSkillFolder) {
  const payload = { userInput }
  if (refineSkillFolder) payload.refineSkillFolder = refineSkillFolder
  return request.post('/skills/refine', payload, { timeout: 180000 })
}
