import { request } from '.'
import { mockAPI } from './mockData'
import { createMockWrapper } from '@/config/mock'

// 定义仪表板数据类型
export interface DashboardStats {
  // 用户统计
  userCount: number
  newUserCount: number
  activeUserCount: number
  
  // 商家统计
  merchantCount: number
  newMerchantCount: number
  activeMerchantCount: number
  
  // 旅行统计
  travelCount: number
  newTravelCount: number
  
  // 动物统计
  animalCount: number
  newAnimalCount: number
  
  // 订单统计
  orderCount: number
  newOrderCount: number
  orderAmount: number
  
  // 系统统计
  onlineUserCount: number
  systemLoad: number
}

export interface UserGrowthData {
  date: string
  count: number
}

export interface OrderStatsData {
  date: string
  count: number
  amount: number
}

export interface ActivityLog {
  id: number
  title: string
  description: string
  avatar: string
  time: string
}

export interface DashboardData {
  stats: DashboardStats
  userGrowth: UserGrowthData[]
  orderStats: OrderStatsData[]
  activities: ActivityLog[]
}

export interface ApiResponse<T = any> {
  success: boolean
  code: number
  message: string
  data: T
}

// 获取仪表板数据
export const getDashboardData = () =>
  request.get<ApiResponse<DashboardData>>('/admin/dashboard')

// 获取用户增长数据
export const getUserGrowthData = (days: number = 7) =>
  request.get<ApiResponse<UserGrowthData[]>>(`/admin/dashboard/user-growth?days=${days}`)

// 获取订单统计数据
export const getOrderStatsData = (days: number = 7) =>
  request.get<ApiResponse<OrderStatsData[]>>(`/admin/dashboard/order-stats?days=${days}`)

// 获取最近活动日志
export const getRecentActivities = () =>
  request.get<ApiResponse<ActivityLog[]>>('/admin/dashboard/activities')

// 开发环境使用模拟数据
const dashboardAPI = createMockWrapper({
  getDashboardData,
  getUserGrowthData,
  getOrderStatsData,
  getRecentActivities
}, mockAPI.dashboard)

export default dashboardAPI