/**
 * 数据大屏 API 及首页统计 API
 */
import request from '@/api/request'

// ==================== 首页统计 API ====================

/**
 * 获取用户统计数据（首页使用）
 */
export function getUserStatistics() {
  return request.get('/api/v1/users/me/statistics')
}

/**
 * 获取最近考试列表（首页使用）
 * @param limit 返回数量，默认5条
 */
export function getRecentExams(limit: number = 5) {
  return request.get('/api/v1/users/me/recent-exams', { limit })
}

// ==================== 数据大屏 API ====================

// 数据概览
export interface DashboardOverview {
  overview: {
    total_users: number
    today_active: number
    week_active: number
    month_active: number
    total_hours: number
    checkin_rate: number
  }
  exam: {
    total_count: number
    pass_rate: number
    avg_score: number
    perfect_users: number
  }
  updated_at: string
}

// 部门对比
export interface DepartmentData {
  id: number
  name: string
  member_count: number
  pass_rate: number
  avg_hours: number
  avg_level: number
}// 学习趋势
export interface TrendData {
  dates: string[]
  trend: Array<{
    date: string
    active_users: number
    learning_hours: number
    exam_count: number
  }>
}

// 等级分布
export interface LevelDistribution {
  levels: number[]
  counts: number[]
}

// 实时动态
export interface ActivityItem {
  id: number
  user_id: number
  user_name: string
  type: string
  description: string
  exp_amount: number
  created_at: string
}

// 课程排行
export interface CourseRanking {
  rank: number
  id: number
  name: string
  description: string
  learners: number
}

// 团队数据
export interface TeamDashboard {
  members: Array<{
    id: number
    username: string
    full_name: string
    avatar_url?: string
    level: number
    total_exp: number
    badge_count: number
  }>
  overview: {
    total_members: number
    avg_level: number
    avg_exp: number
    total_badges: number
  }
  positions: Array<{
    id: number
    name: string
  }>
}

// 完整大屏数据
export interface FullDashboardData {
  overview: DashboardOverview
  departments: DepartmentData[]
  trend: TrendData
  level_distribution: LevelDistribution
  activities: ActivityItem[]
  course_ranking: CourseRanking[]
}

/**
 * 获取企业级数据概览
 */
export function getEnterpriseOverview() {
  return request.get<DashboardOverview>('/dashboard/enterprise/overview')
}

/**
 * 获取部门对比数据
 */
export function getDepartmentComparison() {
  return request.get<DepartmentData[]>('/dashboard/enterprise/departments')
}

/**
 * 获取学习趋势数据
 */
export function getLearningTrend(days = 7) {
  return request.get<TrendData>('/dashboard/enterprise/trend', { params: { days } })
}

/**
 * 获取等级分布数据
 */
export function getLevelDistribution() {
  return request.get<LevelDistribution>('/dashboard/enterprise/level-distribution')
}

/**
 * 获取实时动态
 */
export function getRealtimeActivities(limit = 20) {
  return request.get<ActivityItem[]>('/dashboard/enterprise/activities', { params: { limit } })
}

/**
 * 获取课程热度排行
 */
export function getCourseRanking(limit = 10) {
  return request.get<CourseRanking[]>('/dashboard/enterprise/course-ranking', { params: { limit } })
}

/**
 * 获取团队数据大屏
 */
export function getTeamDashboard() {
  return request.get<TeamDashboard>('/dashboard/team')
}

/**
 * 获取完整大屏数据（一次性加载）
 */
export function getFullDashboardData() {
  return request.get<FullDashboardData>('/dashboard/all')
}
