import request from '@/utils/request'

import type { AxiosPromise } from 'axios'

// ==================== TS 类型定义 ====================

/** 温度看板通用查询参数 */
export interface TempBoardQuery {
  /** 开始时间 yyyy-MM-dd HH:mm:ss */
  startTime?: string
  /** 结束时间 yyyy-MM-dd HH:mm:ss */
  endTime?: string
  /** 测点ID列表 */
  monitorIds?: string[]
  /** 高温阈值（默认35） */
  highTempThreshold?: number
  /** 低温阈值（默认10） */
  lowTempThreshold?: number
  /** 温升过快阈值 ℃/分钟（默认1.0） */
  rapidRiseThreshold?: number
  /** 温度抖动标准差阈值（默认2.0） */
  stddevThreshold?: number
  /** 采样间隔异常阈值 秒（默认300） */
  gapThresholdSeconds?: number
  /** 长时间未更新阈值 秒（默认600） */
  staleThresholdSeconds?: number
  /** TopN 数量（默认10） */
  topN?: number
  /** 单测点趋势查询 */
  monitorId?: string
  /** 预期采样数（完整率计算用） */
  expectedSampleCount?: number
}

/** 测点温度排行项 */
export interface MonitorTempRank {
  monitorId: string
  monitorName: string
  temperature: number
  collectTime: string
}

/** 数据新鲜度项 */
export interface FreshnessItem {
  monitorId: string
  monitorName: string
  temperature: number
  collectTime: string
  ageSeconds: number
}

/** 温度总览 VO */
export interface TempBoardOverviewVO {
  monitorCount: number
  avgLatestTemp: number
  maxLatestTemp: number
  maxTempMonitorId: string
  minLatestTemp: number
  minTempMonitorId: string
  highTempTopN: MonitorTempRank[]
  lowTempTopN: MonitorTempRank[]
  freshnessList: FreshnessItem[]
}

/** 实时监控 VO */
export interface TempBoardRealtimeVO {
  monitorId: string
  monitorName: string
  temperature: number
  collectTime: string
  recodeTime: string
  delaySeconds: number
  staleSeconds: number
}

/** 趋势分析 VO */
export interface TempBoardTrendVO {
  statTime: string
  monitorId: string
  monitorName: string
  avgTemp: number
  maxTemp: number
  minTemp: number
  changeRate: number
  sampleCount: number
  peakTemp: number
  peakTime: string
  valleyTemp: number
  valleyTime: string
  prevTemp: number
  prevTime: string
}

/** 分布分析 VO */
export interface TempBoardDistributionVO {
  tempBucket: string
  tempBin: number
  sampleCount: number
  statDate: string
  statHour: number
  avgTemp: number
  monitorId: string
  monitorName: string
  temperature: number
}

/** 异常预警 VO */
export interface TempBoardAnomalyVO {
  monitorId: string
  monitorName: string
  temperature: number
  collectTime: string
  anomalyType: string
  risePerMin: number
  tempStddev: number
  startTime: string
  endTime: string
  maxTemp: number
  sampleCount: number
  prevTemp: number
  prevTime: string
  statTime: string
}

/** 对比分析 VO */
export interface TempBoardComparisonVO {
  monitorId: string
  monitorName: string
  avgTemp: number
  maxTemp: number
  minTemp: number
  tempRange: number
  tempStddev: number
  statDate: string
  todayAvg: number
  yesterdayAvg: number
  diffValue: number
}

/** 数据质量 VO */
export interface TempBoardQualityVO {
  monitorId: string
  monitorName: string
  delayBucket: string
  sampleCount: number
  collectTime: string
  recodeTime: string
  delaySeconds: number
  prevTime: string
  gapSeconds: number
  actualCount: number
  expectedCount: number
  completenessRate: number
  firstTime: string
  lastTime: string
  temperature: number
}

/** 高级分析 VO */
export interface TempBoardAdvancedVO {
  fromNode: string
  toNode: string
  flowCount: number
  statTime: string
  monitorId: string
  monitorName: string
  avgTemp: number
  sampleCount: number
  tempBucket: string
  maxTemp: number
  minTemp: number
  tempStddev: number
  avgDelay: number
}

// ==================== API 函数 ====================

const BASE_URL = '/ems/report/tempBoard'

// --- A. 温度总览 ---
/** 温度总览 */
export const getTempOverview = (query: TempBoardQuery): AxiosPromise<TempBoardOverviewVO> =>
  request({ url: `${BASE_URL}/overview`, method: 'get', params: query })

// --- B. 实时监控 ---
/** 实时温度明细 */
export const getRealtimeDetail = (query: TempBoardQuery): AxiosPromise<TempBoardRealtimeVO[]> =>
  request({ url: `${BASE_URL}/realtime/detail`, method: 'get', params: query })

/** 高温测点 */
export const getHighTempMonitors = (query: TempBoardQuery): AxiosPromise<TempBoardRealtimeVO[]> =>
  request({ url: `${BASE_URL}/realtime/highTemp`, method: 'get', params: query })

/** 低温测点 */
export const getLowTempMonitors = (query: TempBoardQuery): AxiosPromise<TempBoardRealtimeVO[]> =>
  request({ url: `${BASE_URL}/realtime/lowTemp`, method: 'get', params: query })

/** 长时间未更新测点 */
export const getStaleMonitors = (query: TempBoardQuery): AxiosPromise<TempBoardRealtimeVO[]> =>
  request({ url: `${BASE_URL}/realtime/stale`, method: 'get', params: query })

/** 入库延迟排行 */
export const getDelayRanking = (query: TempBoardQuery): AxiosPromise<TempBoardRealtimeVO[]> =>
  request({ url: `${BASE_URL}/realtime/delay`, method: 'get', params: query })

// --- C. 趋势分析 ---
/** 单测点分钟趋势 */
export const getMinuteTrend = (query: TempBoardQuery): AxiosPromise<TempBoardTrendVO[]> =>
  request({ url: `${BASE_URL}/trend/minute`, method: 'get', params: query })

/** 单测点小时趋势 */
export const getHourlyTrend = (query: TempBoardQuery): AxiosPromise<TempBoardTrendVO[]> =>
  request({ url: `${BASE_URL}/trend/hourly`, method: 'get', params: query })

/** 多测点对比趋势 */
export const getMultiCompareTrend = (query: TempBoardQuery): AxiosPromise<TempBoardTrendVO[]> =>
  request({ url: `${BASE_URL}/trend/multiCompare`, method: 'get', params: query })

/** 日均温趋势 */
export const getDailyTrend = (query: TempBoardQuery): AxiosPromise<TempBoardTrendVO[]> =>
  request({ url: `${BASE_URL}/trend/daily`, method: 'get', params: query })

/** 温度变化率趋势 */
export const getChangeRateTrend = (query: TempBoardQuery): AxiosPromise<TempBoardTrendVO[]> =>
  request({ url: `${BASE_URL}/trend/changeRate`, method: 'get', params: query })

/** 峰谷时刻表 */
export const getPeakValley = (query: TempBoardQuery): AxiosPromise<TempBoardTrendVO[]> =>
  request({ url: `${BASE_URL}/trend/peakValley`, method: 'get', params: query })

// --- D. 分布分析 ---
/** 温度区间分布 */
export const getIntervalDistribution = (query: TempBoardQuery): AxiosPromise<TempBoardDistributionVO[]> =>
  request({ url: `${BASE_URL}/distribution/interval`, method: 'get', params: query })

/** 温度直方图 */
export const getHistogram = (query: TempBoardQuery): AxiosPromise<TempBoardDistributionVO[]> =>
  request({ url: `${BASE_URL}/distribution/histogram`, method: 'get', params: query })

/** 温度箱线图数据 */
export const getBoxplotData = (query: TempBoardQuery): AxiosPromise<TempBoardDistributionVO[]> =>
  request({ url: `${BASE_URL}/distribution/boxplot`, method: 'get', params: query })

/** 日历热力图 */
export const getCalendarHeatmap = (query: TempBoardQuery): AxiosPromise<TempBoardDistributionVO[]> =>
  request({ url: `${BASE_URL}/distribution/calendarHeatmap`, method: 'get', params: query })

/** 小时热力图 */
export const getHourlyHeatmap = (query: TempBoardQuery): AxiosPromise<TempBoardDistributionVO[]> =>
  request({ url: `${BASE_URL}/distribution/hourlyHeatmap`, method: 'get', params: query })

// --- E. 异常预警 ---
/** 高温事件 */
export const getHighTempEvents = (query: TempBoardQuery): AxiosPromise<TempBoardAnomalyVO[]> =>
  request({ url: `${BASE_URL}/anomaly/highTemp`, method: 'get', params: query })

/** 低温事件 */
export const getLowTempEvents = (query: TempBoardQuery): AxiosPromise<TempBoardAnomalyVO[]> =>
  request({ url: `${BASE_URL}/anomaly/lowTemp`, method: 'get', params: query })

/** 连续高温时段 */
export const getContinuousHighTemp = (query: TempBoardQuery): AxiosPromise<TempBoardAnomalyVO[]> =>
  request({ url: `${BASE_URL}/anomaly/continuousHighTemp`, method: 'get', params: query })

/** 温升过快事件 */
export const getRapidRiseEvents = (query: TempBoardQuery): AxiosPromise<TempBoardAnomalyVO[]> =>
  request({ url: `${BASE_URL}/anomaly/rapidRise`, method: 'get', params: query })

/** 温度抖动异常 */
export const getJitterAnomalies = (query: TempBoardQuery): AxiosPromise<TempBoardAnomalyVO[]> =>
  request({ url: `${BASE_URL}/anomaly/jitter`, method: 'get', params: query })

// --- F. 对比分析 ---
/** 测点平均温度排行 */
export const getAvgTempRanking = (query: TempBoardQuery): AxiosPromise<TempBoardComparisonVO[]> =>
  request({ url: `${BASE_URL}/comparison/avgRanking`, method: 'get', params: query })

/** 测点稳定性排行 */
export const getStabilityRanking = (query: TempBoardQuery): AxiosPromise<TempBoardComparisonVO[]> =>
  request({ url: `${BASE_URL}/comparison/stabilityRanking`, method: 'get', params: query })

/** 今日vs昨日对比 */
export const getDailyDiff = (query: TempBoardQuery): AxiosPromise<TempBoardComparisonVO[]> =>
  request({ url: `${BASE_URL}/comparison/dailyDiff`, method: 'get', params: query })

/** 峰值对比 */
export const getPeakCompare = (query: TempBoardQuery): AxiosPromise<TempBoardComparisonVO[]> =>
  request({ url: `${BASE_URL}/comparison/peak`, method: 'get', params: query })

/** 波动幅度对比 */
export const getFluctuationCompare = (query: TempBoardQuery): AxiosPromise<TempBoardComparisonVO[]> =>
  request({ url: `${BASE_URL}/comparison/fluctuation`, method: 'get', params: query })

// --- G. 数据质量 ---
/** 入库延迟分布 */
export const getDelayDistribution = (query: TempBoardQuery): AxiosPromise<TempBoardQualityVO[]> =>
  request({ url: `${BASE_URL}/quality/delayDistribution`, method: 'get', params: query })

/** 时间逆序可疑数据 */
export const getTimeReversal = (query: TempBoardQuery): AxiosPromise<TempBoardQualityVO[]> =>
  request({ url: `${BASE_URL}/quality/timeReversal`, method: 'get', params: query })

/** 采样间隔异常 */
export const getSamplingGapAnomalies = (query: TempBoardQuery): AxiosPromise<TempBoardQualityVO[]> =>
  request({ url: `${BASE_URL}/quality/samplingGap`, method: 'get', params: query })

/** 数据完整率 */
export const getCompletenessRate = (query: TempBoardQuery): AxiosPromise<TempBoardQualityVO[]> =>
  request({ url: `${BASE_URL}/quality/completeness`, method: 'get', params: query })

/** 测点活跃度 */
export const getMonitorActivity = (query: TempBoardQuery): AxiosPromise<TempBoardQualityVO[]> =>
  request({ url: `${BASE_URL}/quality/activity`, method: 'get', params: query })

// --- H. 高级分析 ---
/** 桑基图数据 */
export const getSankeyData = (query: TempBoardQuery): AxiosPromise<TempBoardAdvancedVO[]> =>
  request({ url: `${BASE_URL}/advanced/sankey`, method: 'get', params: query })

/** 主题河流图数据 */
export const getThemeRiverData = (query: TempBoardQuery): AxiosPromise<TempBoardAdvancedVO[]> =>
  request({ url: `${BASE_URL}/advanced/themeRiver`, method: 'get', params: query })

/** 矩形树图数据 */
export const getTreemapData = (query: TempBoardQuery): AxiosPromise<TempBoardAdvancedVO[]> =>
  request({ url: `${BASE_URL}/advanced/treemap`, method: 'get', params: query })

/** 旭日图数据 */
export const getSunburstData = (query: TempBoardQuery): AxiosPromise<TempBoardAdvancedVO[]> =>
  request({ url: `${BASE_URL}/advanced/sunburst`, method: 'get', params: query })

/** 平行坐标图数据 */
export const getParallelData = (query: TempBoardQuery): AxiosPromise<TempBoardAdvancedVO[]> =>
  request({ url: `${BASE_URL}/advanced/parallel`, method: 'get', params: query })
