// API 使用示例文件
// 展示如何在组件中使用各种API接口

import { 
  AuthApi, 
  CourseApi, 
  CommentApi, 
  FavoriteApi, 
  OrderApi, 
  UploadApi, 
  StatisticsApi 
} from '@/api'

// ===== 认证相关示例 =====

// 用户登录示例
export const loginExample = async () => {
  try {
    const response = await AuthApi.login({
      email: 'user@example.com',
      password: 'password123'
    })
    
    if (response.code === 200) {
      const { user, token } = response.data
      // 保存用户信息和token
      localStorage.setItem('token', token)
      localStorage.setItem('user', JSON.stringify(user))
      console.log('登录成功:', user)
    }
  } catch (error) {
    console.error('登录失败:', error)
  }
}

// 用户注册示例
export const registerExample = async () => {
  try {
    const response = await AuthApi.register({
      username: 'newuser',
      email: 'newuser@example.com',
      password: 'password123',
      confirmPassword: 'password123',
      captcha: 'abc123'
    })
    
    if (response.code === 200) {
      console.log('注册成功:', response.data)
    }
  } catch (error) {
    console.error('注册失败:', error)
  }
}

// 获取当前用户信息示例
export const getCurrentUserExample = async () => {
  try {
    const response = await AuthApi.getCurrentUser()
    if (response.code === 200) {
      console.log('用户信息:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('获取用户信息失败:', error)
  }
}

// ===== 课程相关示例 =====

// 获取课程列表示例
export const getCoursesExample = async () => {
  try {
    const response = await CourseApi.getCourses({
      page: 1,
      pageSize: 20,
      categoryId: 1,
      difficulty: 1,
      sortBy: 'createdAt'
    })
    
    if (response.code === 200) {
      const { list, total } = response.data
      console.log('课程列表:', list)
      console.log('总数:', total)
      return response.data
    }
  } catch (error) {
    console.error('获取课程列表失败:', error)
  }
}

// 搜索课程示例
export const searchCoursesExample = async () => {
  try {
    const response = await CourseApi.searchCourses({
      keyword: 'Vue.js',
      category: '前端开发',
      level: 'intermediate',
      price: 'paid',
      rating: 4,
      sortBy: 'newest',
      page: 1,
      pageSize: 10
    })
    
    if (response.code === 200) {
      console.log('搜索结果:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('搜索课程失败:', error)
  }
}

// 获取课程详情示例
export const getCourseDetailExample = async (courseId: number) => {
  try {
    const response = await CourseApi.getCourseById(courseId)
    if (response.code === 200) {
      console.log('课程详情:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('获取课程详情失败:', error)
  }
}

// 报名课程示例
export const enrollCourseExample = async (courseId: number) => {
  try {
    const response = await CourseApi.enrollCourse(courseId)
    if (response.code === 200) {
      console.log('报名成功:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('报名失败:', error)
  }
}

// 获取学习进度示例
export const getLearningProgressExample = async (courseId: number) => {
  try {
    const response = await CourseApi.getLearningProgress(courseId)
    if (response.code === 200) {
      console.log('学习进度:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('获取学习进度失败:', error)
  }
}

// ===== 评论相关示例 =====

// 获取课程评论示例
export const getCourseCommentsExample = async (courseId: number) => {
  try {
    const response = await CommentApi.getCourseComments(courseId, {
      page: 1,
      pageSize: 10,
      sortBy: 'newest'
    })
    
    if (response.code === 200) {
      console.log('课程评论:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('获取课程评论失败:', error)
  }
}

// 添加课程评论示例
export const addCourseCommentExample = async (courseId: number) => {
  try {
    const response = await CommentApi.addCourseComment(courseId, {
      content: '这门课程非常棒，讲解清晰，内容丰富！',
      rating: 5
    })
    
    if (response.code === 200) {
      console.log('评论添加成功:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('添加评论失败:', error)
  }
}

// 点赞评论示例
export const likeCommentExample = async (commentId: number) => {
  try {
    const response = await CommentApi.likeComment(commentId)
    if (response.code === 200) {
      console.log('点赞成功:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('点赞失败:', error)
  }
}

// ===== 收藏相关示例 =====

// 添加收藏示例
export const addFavoriteExample = async (courseId: number) => {
  try {
    const response = await FavoriteApi.addFavorite(courseId)
    if (response.code === 200) {
      console.log('收藏成功:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('收藏失败:', error)
  }
}

// 获取收藏列表示例
export const getFavoritesExample = async () => {
  try {
    const response = await FavoriteApi.getMyFavorites({
      page: 1,
      pageSize: 20,
      sortBy: 'newest'
    })
    
    if (response.code === 200) {
      console.log('收藏列表:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('获取收藏列表失败:', error)
  }
}

// ===== 订单相关示例 =====

// 创建订单示例
export const createOrderExample = async (courseIds: number[]) => {
  try {
    const response = await OrderApi.createOrder({
      courseIds,
      couponCode: 'DISCOUNT10',
      paymentMethod: 'alipay'
    })
    
    if (response.code === 200) {
      console.log('订单创建成功:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('创建订单失败:', error)
  }
}

// 获取订单列表示例
export const getOrdersExample = async () => {
  try {
    const response = await OrderApi.getOrders({
      page: 1,
      pageSize: 10,
      status: 'paid'
    })
    
    if (response.code === 200) {
      console.log('订单列表:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('获取订单列表失败:', error)
  }
}

// ===== 文件上传示例 =====

// 上传头像示例
export const uploadAvatarExample = async (file: File) => {
  try {
    const response = await UploadApi.uploadAvatar(file, (progress) => {
      console.log('上传进度:', progress + '%')
    })
    
    if (response.code === 200) {
      console.log('头像上传成功:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('头像上传失败:', error)
  }
}

// 上传课程视频示例
export const uploadCourseVideoExample = async (file: File, courseId: number) => {
  try {
    const response = await UploadApi.uploadCourseVideo(file, courseId, undefined, (progress) => {
      console.log('视频上传进度:', progress + '%')
    })
    
    if (response.code === 200) {
      console.log('视频上传成功:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('视频上传失败:', error)
  }
}

// ===== 统计相关示例 =====

// 获取平台统计示例
export const getPlatformStatsExample = async () => {
  try {
    const response = await StatisticsApi.getPlatformStats()
    if (response.code === 200) {
      console.log('平台统计:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('获取平台统计失败:', error)
  }
}

// 获取用户学习统计示例
export const getUserLearningStatsExample = async () => {
  try {
    const response = await StatisticsApi.getUserLearningStats()
    if (response.code === 200) {
      console.log('用户学习统计:', response.data)
      return response.data
    }
  } catch (error) {
    console.error('获取用户学习统计失败:', error)
  }
}

// ===== 错误处理示例 =====

// 统一错误处理函数
export const handleApiError = (error: any) => {
  if (error.response) {
    // 服务器返回错误状态码
    const { status, data } = error.response
    switch (status) {
      case 400:
        console.error('请求参数错误:', data.message)
        break
      case 401:
        console.error('未授权访问，请重新登录')
        // 跳转到登录页
        break
      case 403:
        console.error('没有权限访问')
        break
      case 404:
        console.error('请求的资源不存在')
        break
      case 500:
        console.error('服务器内部错误')
        break
      default:
        console.error('请求失败:', data.message || '未知错误')
    }
  } else if (error.request) {
    // 网络错误
    console.error('网络错误，请检查网络连接')
  } else {
    // 其他错误
    console.error('请求配置错误:', error.message)
  }
}
