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

// 服务类型
export type ServiceType = 'database' | 'cache' | 'mq'

// 服务状态
export type ServiceStatus = 'running' | 'stopped'

// 系统服务数据结构
export interface Service {
  id: string
  name: string
  type: ServiceType
  status: ServiceStatus
  host: string
  port: number
  description: string
  created_at: string
  updated_at: string
}

export interface ServiceQueryParams {
  page?: number
  limit?: number
  type?: ServiceType
  status?: ServiceStatus
}

export interface ServiceUpdateData {
  status: ServiceStatus
}

// 获取系统服务列表
export const getServices = (params?: ServiceQueryParams) =>
  request.get<{ success: boolean; code: number; message: string; data: { services: Service[]; pagination: any } }>('/admin/system/services', { params })

// 更新系统服务状态
export const updateServiceStatus = (id: string, data: ServiceUpdateData) =>
  request.put<{ success: boolean; code: number; message: string }>(`/admin/system/services/${id}/status`, data)

// 启动服务
export const startService = (id: string) =>
  request.post<{ success: boolean; code: number; message: string }>(`/admin/system/services/${id}/start`)

// 停止服务
export const stopService = (id: string) =>
  request.post<{ success: boolean; code: number; message: string }>(`/admin/system/services/${id}/stop`)

// 获取系统信息
export const getSystemInfo = () =>
  request.get<{ success: boolean; code: number; message: string; data: any }>('/admin/system/info')

// 获取数据库状态
export const getDatabaseStatus = () =>
  request.get<{ success: boolean; code: number; message: string; data: any }>('/admin/system/database-status')

// 获取缓存状态
export const getCacheStatus = () =>
  request.get<{ success: boolean; code: number; message: string; data: any }>('/admin/system/cache-status')

// 定义系统配置相关类型
export interface SystemConfig {
  id: string
  name: string
  value: string
  type: string
  group: string
  description: string
  created_at: string
  updated_at: string
}

export interface SystemConfigQueryParams {
  page?: number
  limit?: number
  group?: string
}

export interface SystemConfigUpdateData {
  value: string
}

// 获取系统配置列表
export const getSystemConfigs = (params?: SystemConfigQueryParams) =>
  request.get<{ success: boolean; code: number; message: string; data: { configs: SystemConfig[]; pagination: any } }>('/admin/system-configs', { params })

// 更新系统配置
export const updateSystemConfig = (id: string, data: SystemConfigUpdateData) =>
  request.put<{ success: boolean; code: number; message: string }>(`/admin/system-configs/${id}`, data)

// 定义系统统计数据类型
export interface SystemStats {
  userCount: number
  merchantCount: number
  travelCount: number
  animalCount: number
  orderCount: number
  todayUserCount: number
  todayOrderCount: number
}

// 获取系统统计信息
export const getSystemStats = () =>
  request.get<{ success: boolean; code: number; message: string; data: SystemStats }>('/admin/system/stats')

// 定义系统日志相关类型
export interface SystemLog {
  id: string
  level: 'info' | 'warn' | 'error' | 'debug'
  message: string
  timestamp: string
  module: string
  userId?: string
  ip?: string
}

export interface SystemLogQueryParams {
  page?: number
  limit?: number
  level?: string
  module?: string
  startDate?: string
  endDate?: string
}

// 获取系统日志列表
export const getSystemLogs = (params?: SystemLogQueryParams) =>
  request.get<{ success: boolean; code: number; message: string; data: { logs: SystemLog[]; pagination: any } }>('/admin/system/logs', { params })

// 获取系统设置
export const getSystemSettings = () =>
  request.get<{ success: boolean; code: number; message: string; data: any }>('/admin/system/settings')

// 更新系统设置
export const updateSystemSettings = (data: any) =>
  request.put<{ success: boolean; code: number; message: string }>(`/admin/system/settings`, data)

// 定义系统监控数据类型
export interface SystemMonitorData {
  cpuUsage: number
  memoryUsage: number
  networkTraffic: number
  diskUsage: number
  timestamp: string
}

// 获取系统监控数据
export const getSystemMonitorData = () =>
  request.get<{ success: boolean; code: number; message: string; data: SystemMonitorData }>('/admin/system/monitor')

// 开发环境使用模拟数据
const systemAPI = createMockWrapper({
  getServices,
  updateServiceStatus,
  startService,
  stopService,
  getSystemInfo,
  getDatabaseStatus,
  getCacheStatus,
  getSystemConfigs,
  updateSystemConfig,
  getSystemStats,
  getSystemLogs,  // 添加日志API
  getSystemSettings,
  updateSystemSettings,
  getSystemMonitorData
}, mockSystemAPI)

export default systemAPI