// 统计相关API接口
import { ApiRequest } from '../request'
import type { ApiResponse, Statistics } from '../types'

/**
 * 统计API模块
 */
export class StatisticsApi {
  // 获取平台总体统计
  static getPlatformStats(): Promise<ApiResponse<Statistics>> {
    return ApiRequest.get('/statistics/platform')
  }

  // 获取用户学习统计
  static getUserLearningStats(userId?: number): Promise<ApiResponse<{
    totalCourses: number
    completedCourses: number
    inProgressCourses: number
    totalLearningTime: number
    averageProgress: number
    certificates: number
    favoriteCount: number
    commentsCount: number
    learningStreak: number
    lastActiveDate: string
    monthlyProgress: Array<{
      month: string
      coursesCompleted: number
      learningTime: number
    }>
    skillsAcquired: Array<{
      skill: string
      level: number
      coursesCount: number
    }>
    achievements: Array<{
      id: number
      name: string
      description: string
      icon: string
      unlockedAt: string
    }>
  }>> {
    return ApiRequest.get('/statistics/user-learning', { userId })
  }

  // 获取课程统计
  static getCourseStats(courseId: number): Promise<ApiResponse<{
    totalStudents: number
    activeStudents: number
    completedStudents: number
    averageProgress: number
    averageRating: number
    totalRatings: number
    totalComments: number
    totalLearningTime: number
    completionRate: number
    dropoutRate: number
    enrollmentTrend: Array<{
      date: string
      enrollments: number
      completions: number
    }>
    progressDistribution: Array<{
      range: string
      count: number
      percentage: number
    }>
    ratingDistribution: Array<{
      rating: number
      count: number
      percentage: number
    }>
    popularLessons: Array<{
      lessonId: number
      title: string
      viewCount: number
      averageWatchTime: number
    }>
    studentDemographics: {
      ageGroups: Array<{
        range: string
        count: number
        percentage: number
      }>
      locations: Array<{
        country: string
        count: number
        percentage: number
      }>
      devices: Array<{
        device: string
        count: number
        percentage: number
      }>
    }
  }>> {
    return ApiRequest.get(`/statistics/course/${courseId}`)
  }

  // 获取讲师统计
  static getInstructorStats(instructorId: number): Promise<ApiResponse<{
    totalCourses: number
    publishedCourses: number
    totalStudents: number
    totalRevenue: number
    averageRating: number
    totalRatings: number
    totalReviews: number
    followers: number
    coursesRanking: Array<{
      courseId: number
      title: string
      students: number
      rating: number
      revenue: number
    }>
    monthlyStats: Array<{
      month: string
      newStudents: number
      revenue: number
      newCourses: number
    }>
    studentFeedback: {
      positiveCount: number
      neutralCount: number
      negativeCount: number
      commonKeywords: Array<{
        keyword: string
        count: number
        sentiment: 'positive' | 'neutral' | 'negative'
      }>
    }
  }>> {
    return ApiRequest.get(`/statistics/instructor/${instructorId}`)
  }

  // 获取学习进度统计
  static getLearningProgressStats(params?: {
    courseId?: number
    userId?: number
    startDate?: string
    endDate?: string
  }): Promise<ApiResponse<{
    totalSessions: number
    totalLearningTime: number
    averageSessionTime: number
    completionRate: number
    dailyProgress: Array<{
      date: string
      sessions: number
      learningTime: number
      lessonsCompleted: number
    }>
    weeklyProgress: Array<{
      week: string
      sessions: number
      learningTime: number
      lessonsCompleted: number
    }>
    deviceUsage: Array<{
      device: string
      sessions: number
      percentage: number
    }>
    timeDistribution: Array<{
      hour: number
      sessions: number
      percentage: number
    }>
  }>> {
    return ApiRequest.get('/statistics/learning-progress', params)
  }

  // 获取收入统计
  static getRevenueStats(params?: {
    instructorId?: number
    startDate?: string
    endDate?: string
    groupBy?: 'day' | 'week' | 'month' | 'year'
  }): Promise<ApiResponse<{
    totalRevenue: number
    totalOrders: number
    averageOrderValue: number
    refundRate: number
    revenueByPeriod: Array<{
      period: string
      revenue: number
      orders: number
      refunds: number
    }>
    revenueByCategory: Array<{
      category: string
      revenue: number
      percentage: number
    }>
    topSellingCourses: Array<{
      courseId: number
      title: string
      sales: number
      revenue: number
    }>
    paymentMethods: Array<{
      method: string
      count: number
      amount: number
      percentage: number
    }>
  }>> {
    return ApiRequest.get('/statistics/revenue', params)
  }

  // 获取用户行为统计
  static getUserBehaviorStats(params?: {
    startDate?: string
    endDate?: string
  }): Promise<ApiResponse<{
    totalUsers: number
    activeUsers: number
    newUsers: number
    returningUsers: number
    userRetentionRate: number
    averageSessionDuration: number
    bounceRate: number
    userActivity: Array<{
      date: string
      activeUsers: number
      newUsers: number
      sessions: number
    }>
    userEngagement: {
      dailyActiveUsers: number
      weeklyActiveUsers: number
      monthlyActiveUsers: number
      averageSessionsPerUser: number
      averagePageViews: number
    }
    userJourney: Array<{
      step: string
      users: number
      conversionRate: number
    }>
    popularPages: Array<{
      page: string
      views: number
      uniqueViews: number
      averageTime: number
    }>
  }>> {
    return ApiRequest.get('/statistics/user-behavior', params)
  }

  // 获取搜索统计
  static getSearchStats(params?: {
    startDate?: string
    endDate?: string
    limit?: number
  }): Promise<ApiResponse<{
    totalSearches: number
    uniqueSearches: number
    averageResultsPerSearch: number
    noResultsRate: number
    topKeywords: Array<{
      keyword: string
      count: number
      clickThroughRate: number
      conversionRate: number
    }>
    searchTrends: Array<{
      date: string
      searches: number
      uniqueSearches: number
    }>
    categorySearches: Array<{
      category: string
      searches: number
      percentage: number
    }>
    searchSources: Array<{
      source: string
      count: number
      percentage: number
    }>
  }>> {
    return ApiRequest.get('/statistics/search', params)
  }

  // 获取内容统计
  static getContentStats(): Promise<ApiResponse<{
    totalCourses: number
    publishedCourses: number
    draftCourses: number
    totalLessons: number
    totalDuration: string
    averageCourseDuration: string
    averageLessonsPerCourse: number
    contentByCategory: Array<{
      category: string
      courses: number
      lessons: number
      duration: string
    }>
    contentByLevel: Array<{
      level: string
      courses: number
      percentage: number
    }>
    contentByLanguage: Array<{
      language: string
      courses: number
      percentage: number
    }>
    contentGrowth: Array<{
      month: string
      newCourses: number
      newLessons: number
    }>
  }>> {
    return ApiRequest.get('/statistics/content')
  }

  // 获取评论统计
  static getCommentStats(params?: {
    courseId?: number
    instructorId?: number
    startDate?: string
    endDate?: string
  }): Promise<ApiResponse<{
    totalComments: number
    averageRating: number
    responseRate: number
    averageResponseTime: number
    sentimentAnalysis: {
      positive: number
      neutral: number
      negative: number
    }
    ratingDistribution: Array<{
      rating: number
      count: number
      percentage: number
    }>
    commentTrends: Array<{
      date: string
      comments: number
      averageRating: number
    }>
    topReviewedCourses: Array<{
      courseId: number
      title: string
      comments: number
      averageRating: number
    }>
    commonKeywords: Array<{
      keyword: string
      count: number
      sentiment: 'positive' | 'neutral' | 'negative'
    }>
  }>> {
    return ApiRequest.get('/statistics/comments', params)
  }

  // 导出统计报告
  static exportStatsReport(type: string, params?: {
    startDate?: string
    endDate?: string
    format?: 'pdf' | 'excel' | 'csv'
  }): Promise<void> {
    const format = params?.format || 'pdf'
    return ApiRequest.download(`/statistics/export/${type}?format=${format}`, `stats-report.${format}`, params)
  }
}

export default StatisticsApi
