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

// 告警模板类型定义
export interface AlertTemplate {
  id?: number
  name: string
  category: string
  description?: string
  enabled?: boolean
  tags?: string
  rules: AlertRule[]
  channels: ChannelRef[]
  suppression_rule_ids?: number[]
  created_at?: string
  updated_at?: string
}

// 告警规则
export interface AlertRule {
  name: string
  data_source: string
  metric_name: string
  rule_type: 'static' | 'dynamic'
  compare_op: '>' | '>=' | '<' | '<=' | '==' | '!='
  threshold: string | number
  duration: number
  eval_interval: number
  severity_code: string
  baseline_config?: string
  labels?: Record<string, string>
  annotations?: Record<string, string>
}

// 通知渠道引用
export interface ChannelRef {
  channel_id: number
}

// 通知渠道
export interface NotificationChannel {
  id: number
  name: string
  type: string
  config?: Record<string, any>
  enabled?: boolean
  created_at?: string
  updated_at?: string
}

// 抑制规则
export interface SuppressionRule {
  id: number
  name: string
  description?: string
  enabled?: boolean
  matchers?: Record<string, any>
  created_at?: string
  updated_at?: string
}

// 告警级别
export interface AlertSeverity {
  id: number
  code: string
  name: string
  color?: string
  level?: number
  description?: string
  icon?: string
  priority?: number
  enabled?: boolean
  is_default?: boolean
  config?: any
}

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

// 指标聚合响应
export interface MetricsAggregateResponse {
  data_source: string
  server_identity: string
  count: number
  metrics: MetricMeta[]
}

// 模板分类选项
export const TEMPLATE_CATEGORIES = [
  { value: 'os', label: '操作系统监控模板' },
  { value: 'server_hardware', label: '服务器硬件监控模板' },
  { value: 'network_device', label: '网络设备监控模板' },
  { value: 'security_device', label: '安全设备监控模板' },
  { value: 'storage', label: '存储设备监控模板' },
  { value: 'database', label: '数据库监控模板' },
  { value: 'middleware', label: '中间件监控模板' },
  { value: 'virtualization', label: '虚拟化监控模板' },
  { value: 'power_env', label: '电力/UPS/空调/温湿度模板' },
  { value: 'safety_env', label: '消防/门禁/漏水/有害气体模板' },
]

// 数据源选项
export const DATA_SOURCES = [
  { value: 'dc-host', label: '主机/操作系统指标' },
  { value: 'dc-database', label: '数据库指标' },
  { value: 'dc-middleware', label: '中间件指标' },
  { value: 'dc-security', label: '安全设备指标' },
  { value: 'dc-storage', label: '存储设备指标' },
  { value: 'dc-room-device', label: '机房设备指标' },
]

// 比较运算符选项
export const COMPARE_OPERATORS = [
  { value: '>', label: '大于' },
  { value: '>=', label: '大于等于' },
  { value: '<', label: '小于' },
  { value: '<=', label: '小于等于' },
  { value: '==', label: '等于' },
  { value: '!=', label: '不等于' },
]

// 规则类型选项
export const RULE_TYPES = [
  { value: 'static', label: '静态阈值' },
  { value: 'dynamic', label: '动态阈值' },
]

// ==================== 告警模板接口 ====================

/** 获取告警模板列表 */
export const fetchTemplateList = (params?: { page?: number; page_size?: number; name?: string; category?: string; enabled?: boolean }) => {
  return request.get('/Alert/v1/template/list', { params })
}

/** 获取告警模板详情 */
export const fetchTemplateDetail = (id: number) => {
  return request.get(`/Alert/v1/template/get/${id}`)
}

/** 创建告警模板 */
export const createTemplate = (data: AlertTemplate) => {
  return request.post('/Alert/v1/template/create', data)
}

/** 更新告警模板 */
export const updateTemplate = (data: AlertTemplate) => {
  return request.post('/Alert/v1/template/update', data)
}

/** 删除告警模板 */
export const deleteTemplate = (id: number) => {
  return request.delete(`/Alert/v1/template/delete/${id}`)
}

// ==================== 通知渠道接口 ====================

/** 获取通知渠道列表 */
export const fetchChannelList = (params?: { enabled?: boolean; keyword?: string }) => {
  return request.get('/Alert/v1/channel/list', { params })
}

/** 获取通知渠道详情 */
export const fetchChannelDetail = (id: number) => {
  return request.get(`/Alert/v1/channel/get/${id}`)
}

// ==================== 抑制规则接口 ====================

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

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

// ==================== 告警级别接口 ====================

/** 获取告警级别列表 */
export const fetchSeverityList = () => {
  return request.get('/Alert/v1/severity/list')
}

/** 获取告警级别下拉选项（不分页），支持 keyword 模糊搜索与 enabled=true 过滤 */
export const fetchSeverityOptions = (params?: { keyword?: string; enabled?: 'true' | 'false' }) => {
  return request.get('/Alert/v1/severity/options', {
    params: {
      keyword: params?.keyword || undefined,
      enabled: params?.enabled ?? 'true',
    },
  })
}

/** 按 code 获取告警级别 */
export const fetchSeverityByCode = (code: string) => {
  return request.get(`/Alert/v1/severity/get-by-code/${code}`)
}

// ==================== 指标元数据接口 ====================

/** 获取指标元数据 */
export const fetchMetricsMeta = (params: { data_source: string; server_identity?: string; keyword?: string; limit?: number }) => {
  return request.get('/DC-Control/v1/services/metrics/meta', { params })
}
