import request from '@/utils/request';
import type { EmsActionResponse, VibrationBoardQuery } from '../types';

export interface VibrationDistributionPageVO {
  metricField?: string;
  metricLabel?: string;
  unit?: string;
  intervalBuckets?: Array<{ label?: string; count?: number | string }>;
  histogramBuckets?: Array<{ startValue?: number | string; endValue?: number | string; count?: number | string }>;
  calendarHeatmap?: Array<{ statDate?: string; avgValue?: number | string }>;
  hourlyHeatmap?: Array<{ statDate?: string; statHour?: number | string; avgValue?: number | string }>;
}

export interface VibrationOverviewPageVO {
  metricField?: string;
  metricLabel?: string;
  unit?: string;
  sampleCount?: number | string;
  deviceCount?: number | string;
  coverageRate?: number | string;
  metricCards?: Array<{ field?: string; label?: string; unit?: string; latest?: number | string; avg?: number | string; max?: number | string }>;
  gaugeItems?: Array<{ name?: string; value?: number | string; maxValue?: number | string; unit?: string }>;
  primaryMetricStats?: { latest?: number | string; min?: number | string; avg?: number | string; max?: number | string };
  deviceRanks?: Array<{
    monitorId?: string;
    monitorName?: string;
    avg?: number | string;
    latest?: number | string;
    max?: number | string;
    sampleCount?: number | string;
  }>;
}

export interface VibrationTrendPageVO {
  metricField?: string;
  metricLabel?: string;
  unit?: string;
  multiDevice?: boolean;
  series?: Array<{ name?: string; field?: string; unit?: string; points?: Array<{ time?: string; value?: number | string }> }>;
  hourlyItems?: Array<{ hour?: string; avgValue?: number | string }>;
}

export interface VibrationComparisonPageVO {
  metricField?: string;
  metricLabel?: string;
  unit?: string;
  rankItems?: Array<{ monitorId?: string; monitorName?: string; avg?: number | string; latest?: number | string }>;
  scatterItems?: Array<{ monitorId?: string; monitorName?: string; avg?: number | string; max?: number | string; sampleCount?: number | string }>;
}

export interface VibrationQualityPageVO {
  sampleCount?: number | string;
  deviceCount?: number | string;
  coverageRate?: number | string;
  metricQualityItems?: Array<{ field?: string; label?: string; unit?: string; validRate?: number | string; validCount?: number | string }>;
}

export interface VibrationAnomalyPageVO {
  metricField?: string;
  metricLabel?: string;
  unit?: string;
  highThreshold?: number | string;
  warningThreshold?: number | string;
  rapidRiseThreshold?: number | string;
  stddevThreshold?: number | string;
  minContinuousSamples?: number | string;
  highEventCount?: number | string;
  continuousEventCount?: number | string;
  rapidRiseEventCount?: number | string;
  jitterEventCount?: number | string;
  highEvents?: Array<{ monitorId?: string; monitorName?: string; value?: number | string; recodeTime?: string }>;
  continuousEvents?: Array<{
    monitorId?: string;
    monitorName?: string;
    startTime?: string;
    endTime?: string;
    maxValue?: number | string;
    sampleCount?: number | string;
  }>;
  rapidRiseEvents?: Array<{ monitorId?: string; monitorName?: string; diff?: number | string; recodeTime?: string }>;
  jitterEvents?: Array<{ monitorId?: string; monitorName?: string; hourBucket?: string; stddev?: number | string; sampleCount?: number | string }>;
}

export interface VibrationAdvancedPageVO {
  metricField?: string;
  metricLabel?: string;
  unit?: string;
  lowBandUpper?: number | string;
  focusBandUpper?: number | string;
  sankeyNodes?: Array<{ name?: string }>;
  sankeyLinks?: Array<{ source?: string; target?: string; value?: number | string }>;
  treemapItems?: Array<{ name?: string; value?: number | string; levelTag?: string }>;
  parallelAxes?: Array<{ dim?: number | string; name?: string; max?: number | string }>;
  parallelSeries?: Array<{ monitorId?: string; monitorName?: string; values?: Array<number | string> }>;
}

function getPageData<T>(url: string, query?: VibrationBoardQuery): Promise<EmsActionResponse<T>> {
  return request({
    url,
    method: 'get',
    params: query
  });
}

export function getVibrationOverviewData(query?: VibrationBoardQuery): Promise<EmsActionResponse<VibrationOverviewPageVO>> {
  return getPageData<VibrationOverviewPageVO>('/ems/report/vibrationBoard/overview', query);
}

export function getVibrationTrendData(query?: VibrationBoardQuery): Promise<EmsActionResponse<VibrationTrendPageVO>> {
  return getPageData<VibrationTrendPageVO>('/ems/report/vibrationBoard/trend', query);
}

export function getVibrationComparisonData(query?: VibrationBoardQuery): Promise<EmsActionResponse<VibrationComparisonPageVO>> {
  return getPageData<VibrationComparisonPageVO>('/ems/report/vibrationBoard/comparison', query);
}

export function getVibrationQualityData(query?: VibrationBoardQuery): Promise<EmsActionResponse<VibrationQualityPageVO>> {
  return getPageData<VibrationQualityPageVO>('/ems/report/vibrationBoard/quality', query);
}

export function getVibrationDistributionData(query?: VibrationBoardQuery): Promise<EmsActionResponse<VibrationDistributionPageVO>> {
  return getPageData<VibrationDistributionPageVO>('/ems/report/vibrationBoard/distribution', query);
}

export function getVibrationAnomalyData(query?: VibrationBoardQuery): Promise<EmsActionResponse<VibrationAnomalyPageVO>> {
  return getPageData<VibrationAnomalyPageVO>('/ems/report/vibrationBoard/anomaly', query);
}

export function getVibrationAdvancedData(query?: VibrationBoardQuery): Promise<EmsActionResponse<VibrationAdvancedPageVO>> {
  return getPageData<VibrationAdvancedPageVO>('/ems/report/vibrationBoard/advanced', query);
}
