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

// 抑制规则类型
export type SuppressionType = 'dedup' | 'aggregate' | 'dependency' | 'throttle' | 'schedule'

// 抑制规则接口定义
export interface SuppressionRule {
  id?: number
  name: string
  description?: string
  type: SuppressionType
  enabled?: boolean
  priority?: number
  policy_id?: number
  // dedup 类型字段
  dedup_window?: number
  dedup_keys?: string
  // aggregate 类型字段
  aggregate_window?: number
  group_by?: string
  aggregate_count?: number
  // dependency 类型字段
  source_matchers?: string
  target_matchers?: string
  // throttle 类型字段
  throttle_count?: number
  throttle_window?: number
  // schedule 类型字段
  schedule?: string
  // 通用匹配条件
  matchers?: string
  // 运行态统计
  hit_count?: number
  suppressed_count?: number
  last_hit_at?: string
  created_at?: string
  updated_at?: string
}

// 抑制规则列表查询参数
export interface SuppressionListParams {
  page?: number
  page_size?: number
  keyword?: string
  sort?: string
  order?: string
  type?: SuppressionType
  policy_id?: number
  enabled?: boolean
}

// 创建抑制规则参数
export interface SuppressionCreateParams {
  name: string
  type: SuppressionType
  description?: string
  enabled?: boolean
  priority?: number
  policy_id?: number
  dedup_window?: number
  dedup_keys?: string
  aggregate_window?: number
  group_by?: string
  aggregate_count?: number
  source_matchers?: string
  target_matchers?: string
  throttle_count?: number
  throttle_window?: number
  schedule?: string
  matchers?: string
}

// 更新抑制规则参数
export interface SuppressionUpdateParams {
  id: number
  name?: string
  type?: SuppressionType
  description?: string
  enabled?: boolean
  priority?: number
  policy_id?: number
  dedup_window?: number
  dedup_keys?: string
  aggregate_window?: number
  group_by?: string
  aggregate_count?: number
  source_matchers?: string
  target_matchers?: string
  throttle_count?: number
  throttle_window?: number
  schedule?: string
  matchers?: string
}

// 抑制类型选项
export const SUPPRESSION_TYPE_OPTIONS = [
  { value: 'dedup', label: '去重' },
  { value: 'aggregate', label: '聚合' },
  { value: 'dependency', label: '依赖抑制' },
  { value: 'throttle', label: '限流' },
  { value: 'schedule', label: '定时屏蔽' },
]

// 抑制类型标签颜色
export const SUPPRESSION_TYPE_COLORS: Record<SuppressionType, string> = {
  dedup: 'arcoblue',
  aggregate: 'green',
  dependency: 'orangered',
  throttle: 'orange',
  schedule: 'purple',
}

/** 获取抑制规则列表 */
export const fetchSuppressionList = (params?: SuppressionListParams) => {
  return request.get('/Alert/v1/suppression/list', { params })
}

/** 获取抑制规则详情 */
export const fetchSuppressionDetail = (id: number) => {
  return request.get(`/Alert/v1/suppression/get/${id}`)
}

/** 创建抑制规则 */
export const createSuppression = (data: SuppressionCreateParams) => {
  return request.post('/Alert/v1/suppression/create', data)
}

/** 更新抑制规则 */
export const updateSuppression = (data: SuppressionUpdateParams) => {
  return request.post('/Alert/v1/suppression/update', data)
}

/** 删除抑制规则 */
export const deleteSuppression = (id: number) => {
  return request.delete(`/Alert/v1/suppression/delete/${id}`)
}
