/**
 * 文件上传服务
 */
import api from './api'

export interface ReferenceFile {
  name: string
  size: number
  modified?: number
  path?: string
}

export interface UploadResult {
  success: boolean
  message: string
  file?: {
    name: string
    path: string
    size: number
  }
}

export interface FileContentResult {
  success: boolean
  filename: string
  content?: string
  binary?: boolean
  message?: string
}

export const uploadService = {
  /**
   * 上传 Skill 参考文件
   * @param skillId Skill ID
   * @param file 文件对象
   * @returns 上传结果
   */
  uploadReferenceFile: async (skillId: string, file: File): Promise<UploadResult> => {
    const formData = new FormData()
    formData.append('file', file)

    return await api.post<UploadResult>(`/uploads/skills/${skillId}/references`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  /**
   * 列出 Skill 的所有参考文件
   * @param skillId Skill ID
   * @returns 文件列表
   */
  listReferenceFiles: async (skillId: string): Promise<{ skill_id: string; files: ReferenceFile[] }> => {
    return await api.get(`/uploads/skills/${skillId}/references`)
  },

  /**
   * 删除 Skill 参考文件
   * @param skillId Skill ID
   * @param filename 文件名
   * @returns 删除结果
   */
  deleteReferenceFile: async (skillId: string, filename: string): Promise<{ success: boolean; message: string }> => {
    return await api.delete(`/uploads/skills/${skillId}/references/${filename}`)
  },

  /**
   * 读取 Skill 参考文件内容（用于预览）
   * @param skillId Skill ID
   * @param filename 文件名
   * @returns 文件内容
   */
  getReferenceFileContent: async (skillId: string, filename: string): Promise<FileContentResult> => {
    return await api.get(`/uploads/skills/${skillId}/references/${filename}`)
  }
}
