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

/** 安全设备服务项 */
export interface SecurityServiceItem {
  id: number
  created_at: string
  updated_at: string
  deleted_at: string | null
  service_identity: string
  oid: string
  server_identity: string
  name: string
  category: string
  type: string
  description: string
  enabled: boolean
  interval: number
  extra: string
  tags: string
  status_url: string
  agent_config: string
  collect_method: 'api' | 'snmp'
  snmp_target: string
  snmp_port: number
  snmp_version: 'v2c' | 'v3'
  snmp_community: string
  snmp_v3_security_level: string
  snmp_v3_security_name: string
  snmp_v3_auth_protocol: string
  snmp_v3_auth_password: string
  snmp_v3_priv_protocol: string
  snmp_v3_priv_password: string
  snmp_v3_context_name: string
  snmp_timeout_ms: number
  snmp_retries: number
  snmp_oids: string
  collect_on: boolean
  collect_args: string
  collect_interval: number
  collect_last_result: string
  status: string
  status_code: number
  status_message: string
  response_time: number
  last_check_time: string
  last_online_time: string | null
  last_offline_time: string | null
  continuous_errors: number
  uptime: number
}

/** 安全设备服务列表数据 */
export interface SecurityServiceListData {
  total: number
  page: number
  page_size: number
  data: SecurityServiceItem[]
}

/** 安全设备服务列表响应参数 */
export interface SecurityServiceListParams {
  page?: number
  size?: number
  keyword?: string
  enabled?: boolean
}

/** 创建/更新安全设备服务请求参数 */
export interface SecurityServiceFormData {
  service_identity?: string
  oid?: string
  server_identity?: string
  name?: string
  category?: string
  type?: string
  description?: string
  enabled?: boolean
  interval?: number
  extra?: string
  tags?: string
  status_url?: string
  agent_config?: string
  collect_method?: 'api' | 'snmp'
  snmp_target?: string
  snmp_port?: number
  snmp_version?: 'v2c' | 'v3'
  snmp_community?: string
  snmp_v3_security_level?: string
  snmp_v3_security_name?: string
  snmp_v3_auth_protocol?: string
  snmp_v3_auth_password?: string
  snmp_v3_priv_protocol?: string
  snmp_v3_priv_password?: string
  snmp_v3_context_name?: string
  snmp_timeout_ms?: number
  snmp_retries?: number
  snmp_oids?: string
  collect_on?: boolean
  collect_args?: string
  collect_interval?: number
  policy_ids?: number[]
}

/** 安全设备指标项 */
export interface SecurityMetric {
  metric_name: string
  metric_value: number
  metric_unit: string
}

/** 安全设备最新指标数据 */
export interface SecurityMetricsLatestData {
  service_identity: string
  latest_timestamp: string | null
  count: number
  metrics: SecurityMetric[]
}

/** 获取安全设备服务列表（分页） */
export const fetchSecurityServiceList = (params?: SecurityServiceListParams) => {
  return request.get<{ code: number; message: string; details: SecurityServiceListData }>('/DC-Control/v1/security', { params })
}

/** 获取安全设备服务详情 */
export const fetchSecurityServiceDetail = (id: number) => {
  return request.get<{ code: number; message: string; details: SecurityServiceItem }>(`/DC-Control/v1/security/${id}`)
}

/** 创建安全设备服务 */
export const createSecurityService = (data: SecurityServiceFormData) => {
  return request.post<{ code: number; message: string; details: { message: string; id: number } }>('/DC-Control/v1/security', data)
}

/** 更新安全设备服务 */
export const updateSecurityService = (id: number, data: Partial<SecurityServiceFormData>) => {
  return request.put<{ code: number; message: string; details: { message: string } }>(`/DC-Control/v1/security/${id}`, data)
}

/** 删除安全设备服务 */
export const deleteSecurityService = (id: number) => {
  return request.delete<{ code: number; message: string; details: { message: string } }>(`/DC-Control/v1/security/${id}`)
}

/** 获取最新时序指标 */
export const fetchSecurityMetricsLatest = (serviceIdentity: string) => {
  return request.get<{ code: number; message: string; details: SecurityMetricsLatestData }>('/DC-Control/v1/security/metrics/latest', {
    params: { service_identity: serviceIdentity },
  })
}

/** 采集配置补丁参数 */
export interface SecurityServicePatchData {
  collect_method?: 'api' | 'snmp'
  agent_config?: string
  snmp_target?: string
  snmp_port?: number
  snmp_version?: 'v2c' | 'v3'
  snmp_community?: string
  snmp_v3_security_level?: string
  snmp_v3_security_name?: string
  snmp_v3_auth_protocol?: string
  snmp_v3_auth_password?: string
  snmp_v3_priv_protocol?: string
  snmp_v3_priv_password?: string
  snmp_v3_context_name?: string
  snmp_timeout_ms?: number
  snmp_retries?: number
  snmp_oids?: string
  collect_on?: boolean
  collect_interval?: number
}

/** 采集配置补丁 */
export const patchSecurityServiceCollect = (id: number, data: SecurityServicePatchData) => {
  return request.patch<{ code: number; message: string; details: { message: string } }>(`/DC-Control/v1/security/${id}/collect`, data)
}

/** 告警聚合查询参数 */
export interface SecurityMetricsAggregateParams {
  service_identity: string
  metric_name: string
  start_time: string
  end_time: string
  aggregation: 'avg' | 'max' | 'min' | 'sum' | 'count'
}

/** 告警聚合值 */
export interface SecurityMetricsAggregateData {
  metric_name: string
  aggregation: string
  value: number
  unit: string
  start_time: string
  end_time: string
  count: number
}

/** 获取告警聚合值 */
export const fetchSecurityMetricsAggregate = (params: SecurityMetricsAggregateParams) => {
  return request.get<{ code: number; message: string; details: SecurityMetricsAggregateData }>(
    '/DC-Control/v1/services/metrics/security/aggregate',
    { params }
  )
}

/** 安全设备类型映射 */
export const SECURITY_TYPE_MAP: Record<string, string> = {
  firewall: '防火墙',
  waf: 'WAF',
  ids: 'IDS',
  ips: 'IPS',
  vpn: 'VPN',
  other: '其他',
}

/** 安全设备类型选项 */
export const SECURITY_TYPE_OPTIONS = [
  { label: '防火墙', value: 'firewall' },
  { label: 'WAF', value: 'waf' },
  { label: 'IDS', value: 'ids' },
  { label: 'IPS', value: 'ips' },
  { label: 'VPN', value: 'vpn' },
  { label: '其他', value: 'other' },
]

/** 运行状态映射 */
export const STATUS_MAP: Record<string, string> = {
  online: '在线',
  offline: '离线',
  error: '异常',
  unknown: '未知',
}
