import { http } from '@/api/request'
import type { PageQuery, PageResult } from '@/types/api'
import type { AxiosRequestConfig } from 'axios'
import type {
  ProcessEfficiency,
  NodeStayStat,
  StuckInstance,
  AnalysisOverview,
  StatusDistribution,
  Trend,
  EfficiencyQuery,
  OverviewQuery,
  StuckInstanceQuery,
} from '@/types/analysis'

export function getOverview(params?: OverviewQuery, config?: AxiosRequestConfig): Promise<AnalysisOverview> {
  return http.get('/analysis/overview', { params, ...config })
}

export function getStatusDistribution(
  params?: { processDefinitionId?: number; startTime?: string; endTime?: string },
  config?: AxiosRequestConfig
): Promise<StatusDistribution[]> {
  return http.get('/analysis/status-distribution', { params, ...config })
}

export function getTrend(processDefinitionId?: number, config?: AxiosRequestConfig): Promise<Trend[]> {
  return http.get('/analysis/trend', {
    params: processDefinitionId !== undefined ? { processDefinitionId } : undefined,
    ...config,
  })
}

export function getCompletedEfficiency(
  params: EfficiencyQuery,
  config?: AxiosRequestConfig
): Promise<ProcessEfficiency[]> {
  return http.get('/analysis/completed-efficiency', { params, ...config })
}

export function getInProgressByNode(
  processDefinitionId?: number,
  config?: AxiosRequestConfig
): Promise<NodeStayStat[]> {
  return http.get('/analysis/in-progress-by-node', {
    params: processDefinitionId !== undefined ? { processDefinitionId } : undefined,
    ...config,
  })
}

export function getStuckInstances(
  params: StuckInstanceQuery & PageQuery,
  config?: AxiosRequestConfig
): Promise<PageResult<StuckInstance>> {
  return http.get('/analysis/stuck-instances', { params, ...config })
}

export function getNodeCount(processDefinitionId: number): Promise<{ nodeName: string; count: number }[]> {
  return http.get('/analysis/node-count', { params: { processDefinitionId } })
}
