import { request } from '@/api/request'

/** 中间件服务项 */
export interface MiddlewareItem {
  id: number
  created_at: string
  updated_at: string
  deleted_at: string | null
  service_identity: string
  server_identity: string
  name: string
  category: string
  type: string
  description: string
  enabled: boolean
  interval: number
  extra: string
  tags: string
  status_url: string
  status: string
  status_code: number
  status_message: string
  response_time: number
  last_check_time: string
  last_online_time: string | null
  last_offline_time: string | null
  continuous_errors: number
  uptime: number
  agent_config: string
  collect_on: boolean
  collect_args: string
  collect_interval: number
  collect_last_result: string
}

/** 中间件列表响应 */
export interface MiddlewareListResponse {
  total: number
  page: number
  page_size: number
  data: MiddlewareItem[]
}

/** 中间件列表请求参数 */
export interface MiddlewareListParams {
  page?: number
  size?: number
  keyword?: string
  enabled?: boolean
}

/** 创建中间件请求参数 */
export interface MiddlewareCreateData {
  service_identity?: string
  server_identity?: string
  name: string
  category: string
  type: string
  description?: string
  enabled?: boolean
  interval?: number
  extra?: string
  tags?: string
  status_url?: string
  agent_config?: string
  collect_on?: boolean
  collect_args?: string
  collect_interval?: number
  policy_ids?: number[]
}

/** 更新中间件请求参数 */
export interface MiddlewareUpdateData {
  service_identity?: string
  server_identity?: string
  name?: string
  category?: string
  type?: string
  description?: string
  enabled?: boolean
  interval?: number
  extra?: string
  tags?: string
  status_url?: string
  agent_config?: string
  collect_on?: boolean
  collect_args?: string
  collect_interval?: number
  collect_last_result?: string
  status?: string
  status_code?: number
  status_message?: string
  response_time?: number
  last_check_time?: string
  last_online_time?: string | null
  last_offline_time?: string | null
  continuous_errors?: number
  uptime?: number
  policy_ids?: number[]
}

/** 采集配置更新参数 */
export interface MiddlewareCollectData {
  collect_on?: boolean
  collect_args?: string
  collect_interval?: number
  collect_last_result?: string
}

/** 指标上报项 */
export interface MetricItem {
  service_identity: string
  timestamp: string
  [key: string]: any
}

/** 指标上报请求参数 */
export interface MetricsUploadData {
  metrics: MetricItem[]
}

/** 指标上报响应 */
export interface MetricsUploadResponse {
  message: string
  count: number
}

/** 指标元数据项 */
export interface MetricMetaItem {
  metric_name: string
  metric_unit: string
  type: string
  last_timestamp: string
}

/** 指标元数据响应 */
export interface MetricsMetaResponse {
  data_source: string
  collector_identity: string
  server_identity: string
  count: number
  metrics: MetricMetaItem[]
}

/** 指标元数据请求参数 */
export interface MetricsMetaParams {
  data_source: string
  server_identity?: string
  keyword?: string
  limit?: number
}

/** 获取中间件服务列表（匿名接口） */
export const fetchMiddlewareList = (params?: MiddlewareListParams) => {
  return request.get<MiddlewareListResponse>('/DC-Control/v1/middleware', { params })
}

/** 获取中间件服务详情 */
export const fetchMiddlewareDetail = (id: number) => {
  return request.get<MiddlewareItem>(`/DC-Control/v1/middleware/${id}`)
}

/** 创建中间件服务 */
export const createMiddleware = (data: MiddlewareCreateData) => {
  return request.post<{ message: string; id: number }>('/DC-Control/v1/middleware', data)
}

/** 更新中间件服务 */
export const updateMiddleware = (id: number, data: MiddlewareUpdateData) => {
  return request.put<{ message: string }>(`/DC-Control/v1/middleware/${id}`, data)
}

/** 部分更新采集配置 */
export const patchMiddlewareCollect = (id: number, data: MiddlewareCollectData) => {
  return request.patch<{ message: string }>(`/DC-Control/v1/middleware/${id}/collect`, data)
}

/** 删除中间件服务 */
export const deleteMiddleware = (id: number) => {
  return request.delete<{ message: string }>(`/DC-Control/v1/middleware/${id}`)
}

/** 上报中间件服务指标（匿名接口） */
export const uploadMiddlewareMetrics = (data: MetricsUploadData) => {
  return request.post<MetricsUploadResponse>('/DC-Control/v1/middleware/metrics/upload', data)
}

/** 查询中间件服务指标元数据 */
export const fetchMiddlewareMetricsMeta = (params: MetricsMetaParams) => {
  return request.get<MetricsMetaResponse>('/DC-Control/v1/services/metrics/meta', { params })
}