import {
  SUPPRESSION_TYPE_OPTIONS,
  SUPPRESSION_TYPE_COLORS,
  type SuppressionType,
  type SuppressionRule,
  type SuppressionUpdateParams,
  type SuppressionCreateParams,
} from '@/api/ops/suppression'

/**
 * 获取抑制类型标签
 */
export const getTypeLabel = (type: SuppressionType) => {
  const option = SUPPRESSION_TYPE_OPTIONS.find((o) => o.value === type)
  return option?.label || type
}

/**
 * 获取抑制类型颜色
 */
export const getTypeColor = (type: SuppressionType) => {
  return SUPPRESSION_TYPE_COLORS[type] || 'gray'
}

/**
 * 获取关键配置摘要
 */
export const getConfigSummary = (record: SuppressionRule) => {
  switch (record.type) {
    case 'dedup':
      return `窗口 ${record.dedup_window || 0}s · 键 ${record.dedup_keys || '-'}`
    case 'aggregate':
      return `窗口 ${record.aggregate_window || 0}s · 分组 ${record.group_by || '-'} · 阈值 ${record.aggregate_count || 0}`
    case 'throttle':
      return `${record.throttle_count || 0} 次 / ${record.throttle_window || 0}s`
    case 'schedule':
      return '已配置时间窗'
    case 'dependency':
      return '源/目标匹配器已配置'
    default:
      return '-'
  }
}

/**
 * 格式化日期时间
 */
export const formatDateTime = (dateStr?: string) => {
  if (!dateStr) return '-'
  try {
    const date = new Date(dateStr)
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit',
    })
  } catch {
    return dateStr
  }
}

/**
 * 校验 JSON 字段
 * @returns 错误信息，如果校验通过则返回 null
 */
export const validateJsonFields = (formData: SuppressionRule, fields: string[]): string | null => {
  for (const field of fields) {
    const value = formData[field as keyof SuppressionRule]
    if (value && typeof value === 'string' && value !== '{}' && value.trim() !== '') {
      try {
        JSON.parse(value)
      } catch {
        return `${field} 字段 JSON 格式不正确`
      }
    }
  }
  return null
}

/**
 * 构建更新数据（只传非空字段）
 */
export const buildUpdateData = (formData: SuppressionRule): SuppressionUpdateParams => {
  const updateData: SuppressionUpdateParams = { id: formData.id! }

  if (formData.name) updateData.name = formData.name
  if (formData.type) updateData.type = formData.type
  if (formData.description) updateData.description = formData.description
  updateData.enabled = formData.enabled
  if (formData.priority && formData.priority > 0) updateData.priority = formData.priority
  if (formData.policy_id && formData.policy_id > 0) updateData.policy_id = formData.policy_id
  if (formData.dedup_window && formData.dedup_window > 0) updateData.dedup_window = formData.dedup_window
  if (formData.dedup_keys) updateData.dedup_keys = formData.dedup_keys
  if (formData.aggregate_window && formData.aggregate_window > 0) updateData.aggregate_window = formData.aggregate_window
  if (formData.group_by) updateData.group_by = formData.group_by
  if (formData.aggregate_count && formData.aggregate_count > 0) updateData.aggregate_count = formData.aggregate_count
  if (formData.source_matchers) updateData.source_matchers = formData.source_matchers
  if (formData.target_matchers) updateData.target_matchers = formData.target_matchers
  if (formData.throttle_count && formData.throttle_count > 0) updateData.throttle_count = formData.throttle_count
  if (formData.throttle_window && formData.throttle_window > 0) updateData.throttle_window = formData.throttle_window
  if (formData.schedule) updateData.schedule = formData.schedule
  if (formData.matchers) updateData.matchers = formData.matchers

  return updateData
}

/**
 * 构建创建数据
 */
export const buildCreateData = (formData: SuppressionRule): SuppressionCreateParams => {
  const createData: SuppressionCreateParams = {
    name: formData.name,
    type: formData.type,
    enabled: formData.enabled,
  }

  if (formData.description) createData.description = formData.description
  if (formData.priority) createData.priority = formData.priority
  if (formData.policy_id !== undefined) createData.policy_id = formData.policy_id
  if (formData.dedup_window) createData.dedup_window = formData.dedup_window
  if (formData.dedup_keys) createData.dedup_keys = formData.dedup_keys
  if (formData.aggregate_window) createData.aggregate_window = formData.aggregate_window
  if (formData.group_by) createData.group_by = formData.group_by
  if (formData.aggregate_count) createData.aggregate_count = formData.aggregate_count
  if (formData.source_matchers) createData.source_matchers = formData.source_matchers
  if (formData.target_matchers) createData.target_matchers = formData.target_matchers
  if (formData.throttle_count) createData.throttle_count = formData.throttle_count
  if (formData.throttle_window) createData.throttle_window = formData.throttle_window
  if (formData.schedule) createData.schedule = formData.schedule
  if (formData.matchers) createData.matchers = formData.matchers

  return createData
}