import request from '@/utils/request'

// 仪表盘统计数据类型定义
export interface UserStats {
  totalUsers: number
  todayNewUsers: number
  activeUsers: number
  guestUsers: number
}

export interface ContentStats {
  totalConversations: number
  totalMessages: number
  diaryPosts: number
  communityPosts: number
  emotionRecords: number
}

export interface AiServiceStats {
  totalApiCalls: number
  todayApiCalls: number
  successfulCalls: number
  failedCalls: number
  avgResponseTime: number
  aiConfigCount: number
}

export interface SystemStats {
  adminCount: number
  achievementCount: number
  rewardCount: number
  uptime: string
}

export interface RecentActivity {
  type: string
  description: string
  userId: string
  username: string
  activityTime: string
  extraData: Record<string, any>
}

export interface UserGrowthTrend {
  date: string
  newUsers: number
  totalUsers: number
}

export interface RecentLogin {
  userId: string
  username: string
  nickname: string
  avatar: string
  loginTime: string
  timeDescription: string
}

export interface DashboardStats {
  userStats: UserStats
  contentStats: ContentStats
  aiServiceStats: AiServiceStats
  systemStats: SystemStats
  recentActivities: RecentActivity[]
  userGrowthTrends: UserGrowthTrend[]
  recentLogins: RecentLogin[]
  updateTime: string
}

/**
 * AI配置调用次数统计项类型定义
 */
export interface AiConfigCallStatsItem {
  configId: string
  configName: string
  configKey: string
  provider: string
  configType: string
  workflowId: string
  isEnabled: number
  isDefault: number
  callCount: number
}

/**
 * AI配置调用次数统计响应类型定义
 */
export interface AiConfigCallStatsResponse {
  items: AiConfigCallStatsItem[]
}

/**
 * 获取仪表盘统计数据
 */
export function getDashboardStats() {
  return request<DashboardStats>({
    url: '/admin/dashboard/stats',
    method: 'get'
  })
}

/**
 * 获取用户统计数据
 */
export function getUserStats() {
  return request<UserStats>({
    url: '/admin/dashboard/user-stats',
    method: 'get'
  })
}

/**
 * 获取内容统计数据
 */
export function getContentStats() {
  return request<ContentStats>({
    url: '/admin/dashboard/content-stats',
    method: 'get'
  })
}

/**
 * 获取AI服务统计数据
 */
export function getAiServiceStats() {
  return request<AiServiceStats>({
    url: '/admin/dashboard/ai-stats',
    method: 'get'
  })
}

/**
 * 获取系统统计数据
 */
export function getSystemStats() {
  return request<SystemStats>({
    url: '/admin/dashboard/system-stats',
    method: 'get'
  })
}

/**
 * 获取用户增长趋势数据
 */
export function getUserGrowthTrends(days: number = 7) {
  return request<UserGrowthTrend[]>({
    url: '/admin/dashboard/user-growth-trends',
    method: 'get',
    params: { days }
  })
}

/**
 * 获取最近登录用户
 */
export function getRecentLogins(limit: number = 10) {
  return request<RecentLogin[]>({
    url: '/admin/dashboard/recent-logins',
    method: 'get',
    params: { limit }
  })
}

/**
 * 获取 AI 配置调用次数统计（按调用次数倒序）
 * @param limit 返回条数，默认 10
 */
export function getAiConfigCallStats(limit: number = 10) {
  return request<AiConfigCallStatsResponse>({
    url: '/admin/dashboard/aiConfigCallStats',
    method: 'get',
    params: { limit }
  })
}