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

/** 获取 告警策略列表 */
export const fetchPolicyList = (data: {
  page?: number
  page_size?: number
  keyword?: string
  enabled?: boolean
  priority?: number
  created_at_start?: string
  created_at_end?: string
  order_by?: string
  order?: string
}) => request.get('/Alert/v1/policy/list', { params: data })

/** 获取 告警策略详情 */
export const fetchPolicyDetail = (id: number) => request.get(`/Alert/v1/policy/get/${id}`)

/** 创建 告警策略 */
export const createPolicy = (data: {
  name: string
  description?: string
  enabled?: boolean
  priority?: number
  labels?: string
  template_id?: number
  auto_create_ticket?: boolean
  feedback_template_id?: number
  dispatch_rule?: string
}) => request.post('/Alert/v1/policy/create', data)

/** 更新 告警策略 */
export const updatePolicy = (data: {
  id: number
  name?: string
  description?: string
  enabled?: boolean
  priority?: number
  labels?: string
  template_id?: number
  auto_create_ticket?: boolean
  feedback_template_id?: number
  dispatch_rule?: string
}) => request.post('/Alert/v1/policy/update', data)

/** 删除 告警策略 */
export const deletePolicy = (id: number) => request.delete(`/Alert/v1/policy/delete/${id}`)

/** 获取 告警规则列表 */
export const fetchRuleList = (data: {
  policy_id?: number
  page?: number
  page_size?: number
  keyword?: string
  sort?: string
  order?: string
}) => request.get('/Alert/v1/rule/list', { params: data })

/** 获取 告警规则详情 */
export const fetchRuleDetail = (id: number) => request.get(`/Alert/v1/rule/get/${id}`)

/** 创建 告警规则 */
export const createRule = (data: {
  policy_id: number
  name: string
  description?: string
  rule_type: string
  severity_id: number
  enabled?: boolean
  metric_name?: string
  data_source?: string
  threshold?: number
  compare_op?: string
  duration?: number
  eval_interval?: number
  baseline_config?: string
  labels?: string
  annotations?: string
}) => request.post('/Alert/v1/rule/create', data)

/** 更新 告警规则 */
export const updateRule = (data: {
  id: number
  name?: string
  description?: string
  rule_type?: string
  severity_id?: number
  enabled?: boolean
  metric_name?: string
  data_source?: string
  threshold?: number
  compare_op?: string
  duration?: number
  eval_interval?: number
  baseline_config?: string
  labels?: string
  annotations?: string
}) => request.post('/Alert/v1/rule/update', data)

/** 删除 告警规则 */
export const deleteRule = (id: number) => request.delete(`/Alert/v1/rule/delete/${id}`)

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

/** 获取 告警级别列表 */
export const fetchSeverityList = (data: { page?: number; page_size?: number; enabled?: string }) =>
  request.get('/Alert/v1/severity/list', { params: data })

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

/** 获取工单模板下拉选项 */
export const fetchFeedbackTemplateOptions = (data?: { status?: 'active' | 'inactive' }) =>
  request.get('/Feedback/v1/templates/options', { params: data || {} })

/** 告警策略选项项 */
export interface PolicyOptionItem {
  id: number
  created_at: string
  updated_at: string
  name: string
  description: string
  enabled: boolean
  priority: number
  labels: string
  template_id: number
  auto_create_ticket: boolean
  feedback_template_id: number
  dispatch_rule: string
}

/** 告警策略选项查询参数 */
export interface PolicyOptionsParams {
  keyword?: string
  enabled?: boolean
}

/** 获取告警策略下拉选项（不分页），支持 keyword 模糊搜索与 enabled 过滤 */
export const fetchPolicyOptions = (params?: PolicyOptionsParams) => request.get<PolicyOptionItem[]>('/Alert/v1/policy/options', { params })
