/**
 * 课程资料相关API
 */
import request from '@/utils/http'
import type { Material, PreviewInfo } from '@/types/material'

/**
 * 获取课程资料列表
 */
export function getMaterials(courseId: number) {
  return request.get<{
    code: number
    message: string
    data: Material[]
  }>(`/api/v1/courses/${courseId}/materials`)
}

/**
 * 获取资料预览信息
 */
export function getPreview(materialId: number) {
  return request.get<{
    code: number
    message: string
    data: PreviewInfo
  }>(`/api/v1/preview/material/${materialId}`)
}

/**
 * 下载资料
 */
export function downloadMaterial(materialId: number, fileName: string) {
  // 创建隐藏的a标签触发下载
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = `/api/v1/preview/material/${materialId}`
  link.download = fileName
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

/**
 * 直接下载文件（通过URL）
 */
export function downloadFile(fileUrl: string, fileName: string) {
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = fileUrl
  link.download = fileName
  link.target = '_blank'
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

/**
 * 检查转换服务状态（调试用）
 */
export function checkConverterStatus() {
  return request.get<{
    code: number
    message: string
    data: {
      libreoffice_installed: boolean
      libreoffice_version: string | null
      supported_formats: string[]
      converted_path: string
    }
  }>('/api/v1/preview/check-converter')
}

