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

export interface DisplacementDistributionPageVO {
  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 DisplacementOverviewPageVO {
  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 DisplacementTrendPageVO {
  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 DisplacementComparisonPageVO {
  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 DisplacementQualityPageVO {
  sampleCount?: number | string;
  deviceCount?: number | string;
  coverageRate?: number | string;
  validCount?: number | string;
  invalidCount?: number | string;
  validRate?: number | string;
  invalidRate?: number | string;
  metricQualityItems?: Array<{ field?: string; label?: string; unit?: string; validRate?: number | string; validCount?: number | string }>;
}

export interface DisplacementAnomalyPageVO {
  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 DisplacementAdvancedPageVO {
  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?: DisplacementBoardQuery): Promise<EmsActionResponse<T>> {
  return request({
    url,
    method: 'get',
    params: query
  });
}

export function getDisplacementOverviewData(query?: DisplacementBoardQuery): Promise<EmsActionResponse<DisplacementOverviewPageVO>> {
  return getPageData<DisplacementOverviewPageVO>('/ems/report/displacementBoard/overview', query);
}

export function getDisplacementTrendData(query?: DisplacementBoardQuery): Promise<EmsActionResponse<DisplacementTrendPageVO>> {
  return getPageData<DisplacementTrendPageVO>('/ems/report/displacementBoard/trend', query);
}

export function getDisplacementComparisonData(query?: DisplacementBoardQuery): Promise<EmsActionResponse<DisplacementComparisonPageVO>> {
  return getPageData<DisplacementComparisonPageVO>('/ems/report/displacementBoard/comparison', query);
}

export function getDisplacementQualityData(query?: DisplacementBoardQuery): Promise<EmsActionResponse<DisplacementQualityPageVO>> {
  return getPageData<DisplacementQualityPageVO>('/ems/report/displacementBoard/quality', query);
}

export function getDisplacementDistributionData(query?: DisplacementBoardQuery): Promise<EmsActionResponse<DisplacementDistributionPageVO>> {
  return getPageData<DisplacementDistributionPageVO>('/ems/report/displacementBoard/distribution', query);
}

export function getDisplacementAnomalyData(query?: DisplacementBoardQuery): Promise<EmsActionResponse<DisplacementAnomalyPageVO>> {
  return getPageData<DisplacementAnomalyPageVO>('/ems/report/displacementBoard/anomaly', query);
}

export function getDisplacementAdvancedData(query?: DisplacementBoardQuery): Promise<EmsActionResponse<DisplacementAdvancedPageVO>> {
  return getPageData<DisplacementAdvancedPageVO>('/ems/report/displacementBoard/advanced', query);
}
