/**
 * 服务监测 - 统计 API
 */
import http from '@/utils/http'
import type {
  OverviewStats,
  ModuleStats,
  ItemTrendData,
  AbnormalItem,
  ItemKeysModule,
  HealthTrendData,
} from '@/types/service-monitor'

interface StatsParams {
  target_id?: string
  start_date?: string
  end_date?: string
}

/** 获取概览统计 */
export async function getOverviewStats(
  params?: StatsParams
): Promise<{ success: boolean; data: OverviewStats }> {
  const res = await http.get<{ success: boolean; data: OverviewStats }>(
    '/api/service-monitor/statistics/overview',
    { params }
  )
  return res.data
}

/** 获取模块健康统计 */
export async function getModuleStats(
  params?: StatsParams
): Promise<{ success: boolean; data: { modules: ModuleStats[] } }> {
  const res = await http.get<{ success: boolean; data: { modules: ModuleStats[] } }>(
    '/api/service-monitor/statistics/modules',
    { params }
  )
  return res.data
}

/** 获取检测项趋势 */
export async function getItemTrend(
  itemKey: string,
  params?: StatsParams
): Promise<{ success: boolean; data: ItemTrendData }> {
  const res = await http.get<{ success: boolean; data: ItemTrendData }>(
    '/api/service-monitor/statistics/item-trend',
    { params: { ...params, item_key: itemKey } }
  )
  return res.data
}

/** 获取高频异常项 */
export async function getAbnormalItems(
  params?: StatsParams
): Promise<{ success: boolean; data: { items: AbnormalItem[] } }> {
  const res = await http.get<{ success: boolean; data: { items: AbnormalItem[] } }>(
    '/api/service-monitor/statistics/abnormal-items',
    { params }
  )
  return res.data
}

/** 获取检测项键列表 */
export async function getItemKeys(
  params?: StatsParams
): Promise<{ success: boolean; data: ItemKeysModule[] }> {
  const res = await http.get<{ success: boolean; data: ItemKeysModule[] }>(
    '/api/service-monitor/statistics/item-keys',
    { params }
  )
  return res.data
}

/** 获取健康度趋势 */
export async function getHealthTrend(
  params?: StatsParams
): Promise<{ success: boolean; data: HealthTrendData }> {
  const res = await http.get<{ success: boolean; data: HealthTrendData }>(
    '/api/service-monitor/statistics/health-trend',
    { params }
  )
  return res.data
}