// 收藏相关API接口
import { ApiRequest } from '../request'
import type {
  ApiResponse,
  PaginationResponse,
  Favorite,
  Course,
} from '../types'

/**
 * 收藏API模块
 */
export class FavoriteApi {
  // 添加收藏
  static addFavorite(courseId: number): Promise<ApiResponse<Favorite>> {
    return ApiRequest.post('/favorites', { courseId })
  }

  // 取消收藏
  static removeFavorite(courseId: number): Promise<ApiResponse<null>> {
    return ApiRequest.delete(`/favorites/${courseId}`)
  }

  // 检查是否已收藏
  static checkFavorite(courseId: number): Promise<ApiResponse<{
    isFavorite: boolean
    favoriteId?: number
  }>> {
    return ApiRequest.get(`/favorites/check/${courseId}`)
  }

  // 获取我的收藏列表
  static getMyFavorites(params?: {
    page?: number
    pageSize?: number
    category?: string
    sortBy?: 'newest' | 'oldest' | 'rating' | 'price'
  }): Promise<ApiResponse<PaginationResponse<Favorite>>> {
    return ApiRequest.get('/favorites', params)
  }

  // 批量添加收藏
  static batchAddFavorites(courseIds: number[]): Promise<ApiResponse<{
    success: number[]
    failed: number[]
    message: string
  }>> {
    return ApiRequest.post('/favorites/batch', { courseIds })
  }

  // 批量取消收藏
  static batchRemoveFavorites(courseIds: number[]): Promise<ApiResponse<{
    success: number[]
    failed: number[]
    message: string
  }>> {
    return ApiRequest.delete('/favorites/batch', { courseIds })
  }

  // 获取收藏统计
  static getFavoriteStats(): Promise<ApiResponse<{
    totalFavorites: number
    favoritesByCategory: Array<{
      category: string
      count: number
    }>
    recentFavorites: Course[]
    favoritesTrend: Array<{
      date: string
      count: number
    }>
  }>> {
    return ApiRequest.get('/favorites/stats')
  }

  // 导出收藏列表
  static exportFavorites(format: 'json' | 'csv' | 'excel'): Promise<void> {
    return ApiRequest.download(`/favorites/export?format=${format}`, `favorites.${format}`)
  }

  // 导入收藏列表
  static importFavorites(file: File): Promise<ApiResponse<{
    imported: number
    failed: number
    duplicates: number
    message: string
  }>> {
    return ApiRequest.upload('/favorites/import', file)
  }

  // 清空收藏列表
  static clearAllFavorites(): Promise<ApiResponse<null>> {
    return ApiRequest.delete('/favorites/clear')
  }

  // 获取收藏夹分类（如果支持分类收藏）
  static getFavoriteFolders(): Promise<ApiResponse<Array<{
    id: number
    name: string
    description?: string
    courseCount: number
    createdAt: string
  }>>> {
    return ApiRequest.get('/favorite-folders')
  }

  // 创建收藏夹
  static createFavoriteFolder(data: {
    name: string
    description?: string
  }): Promise<ApiResponse<{
    id: number
    name: string
    description?: string
  }>> {
    return ApiRequest.post('/favorite-folders', data)
  }

  // 更新收藏夹
  static updateFavoriteFolder(folderId: number, data: {
    name?: string
    description?: string
  }): Promise<ApiResponse<null>> {
    return ApiRequest.put(`/favorite-folders/${folderId}`, data)
  }

  // 删除收藏夹
  static deleteFavoriteFolder(folderId: number): Promise<ApiResponse<null>> {
    return ApiRequest.delete(`/favorite-folders/${folderId}`)
  }

  // 将课程添加到收藏夹
  static addCourseToFolder(courseId: number, folderId: number): Promise<ApiResponse<null>> {
    return ApiRequest.post(`/favorite-folders/${folderId}/courses`, { courseId })
  }

  // 从收藏夹移除课程
  static removeCourseFromFolder(courseId: number, folderId: number): Promise<ApiResponse<null>> {
    return ApiRequest.delete(`/favorite-folders/${folderId}/courses/${courseId}`)
  }

  // 获取收藏夹中的课程
  static getFolderCourses(folderId: number, params?: {
    page?: number
    pageSize?: number
  }): Promise<ApiResponse<PaginationResponse<Course>>> {
    return ApiRequest.get(`/favorite-folders/${folderId}/courses`, params)
  }

  // 移动课程到其他收藏夹
  static moveCourseToFolder(courseId: number, fromFolderId: number, toFolderId: number): Promise<ApiResponse<null>> {
    return ApiRequest.post('/favorites/move', {
      courseId,
      fromFolderId,
      toFolderId
    })
  }

  // 获取最近收藏的课程
  static getRecentFavorites(limit?: number): Promise<ApiResponse<Course[]>> {
    return ApiRequest.get('/favorites/recent', { limit })
  }

  // 获取收藏推荐（基于收藏历史推荐相似课程）
  static getFavoriteRecommendations(limit?: number): Promise<ApiResponse<Course[]>> {
    return ApiRequest.get('/favorites/recommendations', { limit })
  }

  // 分享收藏列表
  static shareFavorites(data: {
    folderId?: number
    isPublic: boolean
    description?: string
  }): Promise<ApiResponse<{
    shareId: string
    shareUrl: string
    expiresAt?: string
  }>> {
    return ApiRequest.post('/favorites/share', data)
  }

  // 获取分享的收藏列表
  static getSharedFavorites(shareId: string): Promise<ApiResponse<{
    title: string
    description?: string
    courses: Course[]
    sharedBy: string
    sharedAt: string
  }>> {
    return ApiRequest.get(`/favorites/shared/${shareId}`)
  }
}

export default FavoriteApi
