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

const BASE = '/Logs/v1'

/** 解析 bsm-sdk 风格响应，取出业务 data */
export function unwrapLogsPayload(res: any): any {
  if (res == null) return null
  if (typeof res.code === 'number' && res.code !== 0) return null
  return res.details ?? res.data ?? null
}

export interface LogEvent {
  id: number
  created_at: string
  source_kind: string
  remote_addr: string
  raw_payload: string
  normalized_summary: string
  normalized_detail: string
  device_name: string
  severity_code: string
  trap_oid: string
  alert_sent: boolean
}

export interface LogEntriesParams {
  page?: number
  page_size?: number
  source_kind?: string
}

export interface LogEntriesResult {
  total: number
  page: number
  page_size: number
  items: LogEvent[]
}

export interface SyslogRule {
  id?: number
  created_at?: string
  updated_at?: string
  name: string
  enabled: boolean
  priority: number
  device_name_contains: string
  keyword_regex: string
  alert_name: string
  severity_code: string
  policy_id: number
}

export interface TrapRule {
  id?: number
  created_at?: string
  updated_at?: string
  name: string
  enabled: boolean
  priority: number
  oid_prefix: string
  varbind_match_regex: string
  alert_name: string
  severity_code: string
  policy_id: number
}

export interface TrapDictionaryEntry {
  id?: number
  created_at?: string
  updated_at?: string
  oid_prefix: string
  title: string
  description: string
  severity_code: string
  recovery_message: string
  enabled: boolean
}

export interface TrapShield {
  id?: number
  created_at?: string
  updated_at?: string
  name: string
  enabled: boolean
  source_ip_cidr: string
  oid_prefix: string
  interface_hint: string
  time_windows_json: string
}

export function fetchLogEntries(params?: LogEntriesParams) {
  return request.get(`${BASE}/entries`, { params })
}

export function fetchSyslogRules() {
  return request.get(`${BASE}/syslog-rules`)
}

export function createSyslogRule(data: Partial<SyslogRule>) {
  return request.post(`${BASE}/syslog-rules`, data)
}

export function updateSyslogRule(id: number, data: Partial<SyslogRule>) {
  return request.put(`${BASE}/syslog-rules/${id}`, data)
}

export function deleteSyslogRule(id: number) {
  return request.delete(`${BASE}/syslog-rules/${id}`)
}

export function fetchTrapRules() {
  return request.get(`${BASE}/trap-rules`)
}

export function createTrapRule(data: Partial<TrapRule>) {
  return request.post(`${BASE}/trap-rules`, data)
}

export function updateTrapRule(id: number, data: Partial<TrapRule>) {
  return request.put(`${BASE}/trap-rules/${id}`, data)
}

export function deleteTrapRule(id: number) {
  return request.delete(`${BASE}/trap-rules/${id}`)
}

export function fetchTrapDictionary() {
  return request.get(`${BASE}/trap-dictionary`)
}

export function createTrapDictionary(data: Partial<TrapDictionaryEntry>) {
  return request.post(`${BASE}/trap-dictionary`, data)
}

export function updateTrapDictionary(id: number, data: Partial<TrapDictionaryEntry>) {
  return request.put(`${BASE}/trap-dictionary/${id}`, data)
}

export function deleteTrapDictionary(id: number) {
  return request.delete(`${BASE}/trap-dictionary/${id}`)
}

export function fetchTrapSuppressions() {
  return request.get(`${BASE}/trap-suppressions`)
}

export function createTrapSuppression(data: Partial<TrapShield>) {
  return request.post(`${BASE}/trap-suppressions`, data)
}

export function updateTrapSuppression(id: number, data: Partial<TrapShield>) {
  return request.put(`${BASE}/trap-suppressions/${id}`, data)
}

export function deleteTrapSuppression(id: number) {
  return request.delete(`${BASE}/trap-suppressions/${id}`)
}
