// 评论相关API接口
import { ApiRequest } from '../request'
import type {
  ApiResponse,
  PaginationResponse,
  Comment,
} from '../types'

/**
 * 评论API模块
 */
export class CommentApi {
  // 获取课程评论
  static getCourseComments(courseId: number, params?: {
    page?: number
    pageSize?: number
    sortBy?: 'newest' | 'oldest' | 'rating' | 'helpful'
    rating?: number
  }): Promise<ApiResponse<PaginationResponse<Comment>>> {
    return ApiRequest.get(`/courses/${courseId}/comments`, params)
  }

  // 获取课时评论
  static getLessonComments(lessonId: number, params?: {
    page?: number
    pageSize?: number
    sortBy?: 'newest' | 'oldest' | 'helpful'
  }): Promise<ApiResponse<PaginationResponse<Comment>>> {
    return ApiRequest.get(`/lessons/${lessonId}/comments`, params)
  }

  // 添加课程评论
  static addCourseComment(courseId: number, data: {
    content: string
    rating?: number
    parentId?: number
  }): Promise<ApiResponse<Comment>> {
    return ApiRequest.post(`/courses/${courseId}/comments`, data)
  }

  // 添加课时评论
  static addLessonComment(lessonId: number, data: {
    content: string
    parentId?: number
  }): Promise<ApiResponse<Comment>> {
    return ApiRequest.post(`/lessons/${lessonId}/comments`, data)
  }

  // 更新评论
  static updateComment(commentId: number, data: {
    content: string
    rating?: number
  }): Promise<ApiResponse<Comment>> {
    return ApiRequest.put(`/comments/${commentId}`, data)
  }

  // 删除评论
  static deleteComment(commentId: number): Promise<ApiResponse<null>> {
    return ApiRequest.delete(`/comments/${commentId}`)
  }

  // 点赞评论
  static likeComment(commentId: number): Promise<ApiResponse<{
    likes: number
    isLiked: boolean
  }>> {
    return ApiRequest.post(`/comments/${commentId}/like`)
  }

  // 取消点赞评论
  static unlikeComment(commentId: number): Promise<ApiResponse<{
    likes: number
    isLiked: boolean
  }>> {
    return ApiRequest.delete(`/comments/${commentId}/like`)
  }

  // 踩评论
  static dislikeComment(commentId: number): Promise<ApiResponse<{
    dislikes: number
    isDisliked: boolean
  }>> {
    return ApiRequest.post(`/comments/${commentId}/dislike`)
  }

  // 取消踩评论
  static undislikeComment(commentId: number): Promise<ApiResponse<{
    dislikes: number
    isDisliked: boolean
  }>> {
    return ApiRequest.delete(`/comments/${commentId}/dislike`)
  }

  // 举报评论
  static reportComment(commentId: number, data: {
    reason: string
    description?: string
  }): Promise<ApiResponse<null>> {
    return ApiRequest.post(`/comments/${commentId}/report`, data)
  }

  // 获取评论回复
  static getCommentReplies(commentId: number, params?: {
    page?: number
    pageSize?: number
  }): Promise<ApiResponse<PaginationResponse<Comment>>> {
    return ApiRequest.get(`/comments/${commentId}/replies`, params)
  }

  // 获取我的评论
  static getMyComments(params?: {
    page?: number
    pageSize?: number
    type?: 'course' | 'lesson'
  }): Promise<ApiResponse<PaginationResponse<Comment>>> {
    return ApiRequest.get('/my-comments', params)
  }

  // 获取评论统计
  static getCommentStats(courseId?: number, lessonId?: number): Promise<ApiResponse<{
    totalComments: number
    averageRating: number
    ratingDistribution: Array<{
      rating: number
      count: number
      percentage: number
    }>
    recentComments: Comment[]
  }>> {
    const params: any = {}
    if (courseId) params.courseId = courseId
    if (lessonId) params.lessonId = lessonId
    
    return ApiRequest.get('/comments/stats', params)
  }

  // 标记评论为有用
  static markCommentHelpful(commentId: number): Promise<ApiResponse<{
    helpfulCount: number
    isHelpful: boolean
  }>> {
    return ApiRequest.post(`/comments/${commentId}/helpful`)
  }

  // 取消标记评论为有用
  static unmarkCommentHelpful(commentId: number): Promise<ApiResponse<{
    helpfulCount: number
    isHelpful: boolean
  }>> {
    return ApiRequest.delete(`/comments/${commentId}/helpful`)
  }

  // 置顶评论（管理员功能）
  static pinComment(commentId: number): Promise<ApiResponse<null>> {
    return ApiRequest.post(`/comments/${commentId}/pin`)
  }

  // 取消置顶评论（管理员功能）
  static unpinComment(commentId: number): Promise<ApiResponse<null>> {
    return ApiRequest.delete(`/comments/${commentId}/pin`)
  }

  // 审核评论（管理员功能）
  static moderateComment(commentId: number, action: 'approve' | 'reject' | 'hide'): Promise<ApiResponse<null>> {
    return ApiRequest.post(`/comments/${commentId}/moderate`, { action })
  }

  // 批量删除评论（管理员功能）
  static batchDeleteComments(commentIds: number[]): Promise<ApiResponse<null>> {
    return ApiRequest.post('/comments/batch-delete', { commentIds })
  }

  // 获取待审核评论（管理员功能）
  static getPendingComments(params?: {
    page?: number
    pageSize?: number
  }): Promise<ApiResponse<PaginationResponse<Comment>>> {
    return ApiRequest.get('/comments/pending', params)
  }

  // 获取被举报的评论（管理员功能）
  static getReportedComments(params?: {
    page?: number
    pageSize?: number
  }): Promise<ApiResponse<PaginationResponse<Comment & {
    reports: Array<{
      id: number
      reason: string
      description?: string
      reportedBy: string
      reportedAt: string
    }>
  }>>> {
    return ApiRequest.get('/comments/reported', params)
  }
}

export default CommentApi
