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

/** 机房设备服务项 */
export interface RoomDeviceItem {
  id: number
  created_at: string
  updated_at: string
  deleted_at: string | null
  service_identity: string
  name: string
  description: string
  room_id: number
  device_code?: string
  device_category: string
  type?: 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
  enabled: boolean
  collect_on: boolean
  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
  policy_ids?: number[]
}

/** 机房设备列表响应 */
export interface RoomDeviceListResponse {
  total: number
  page: number
  page_size: number
  data: RoomDeviceItem[]
}

/** 机房设备列表查询参数 */
export interface RoomDeviceListParams {
  page?: number
  size?: number
  keyword?: string
  enabled?: boolean
  room_id?: string
  device_category?: string
}

/** 机房设备创建数据 */
export interface RoomDeviceCreateData {
  service_identity?: string
  name: string
  description?: string
  room_id: number
  device_category: 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
  enabled?: boolean
  collect_on?: boolean
  collect_interval?: number
  policy_ids?: number[]
}

/** 机房设备更新数据 */
export interface RoomDeviceUpdateData {
  name?: string
  description?: string
  room_id?: number
  device_category?: 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
  enabled?: boolean
  collect_on?: boolean
  collect_interval?: number
  policy_ids?: number[]
}

/** 机房设备采集配置数据 */
export interface RoomDeviceCollectData {
  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 interface MetricItem {
  timestamp: string
  service_identity: string
  room_id: string
  device_category: string
  type?: string
  metric_name: string
  metric_value: number
  metric_unit?: string
}

/** GET /room-devices/metrics/latest 的 details 载荷 */
export interface RoomDeviceMetricsLatestDetails {
  service_identity: string
  latest_timestamp?: string | null
  count: number
  metrics: MetricItem[]
}

/** 统一 API 封装（与 storage 等页一致） */
export interface RoomDeviceMetricsLatestResponse {
  code: number
  message?: string
  details?: RoomDeviceMetricsLatestDetails
}

/** 指标上报数据 */
export interface MetricsUploadData {
  metrics: MetricItem[]
}

/** 获取机房设备列表（分页） */
export const fetchRoomDeviceList = (params?: RoomDeviceListParams) => {
  return request.get<RoomDeviceListResponse>('/DC-Control/v1/room-devices', { params })
}

/** 获取机房设备详情 */
export const fetchRoomDeviceDetail = (id: number) => {
  return request.get<RoomDeviceItem>(`/DC-Control/v1/room-devices/${id}`)
}

/** 创建机房设备 */
export const createRoomDevice = (data: RoomDeviceCreateData) => {
  return request.post<{ message: string; id: number }>('/DC-Control/v1/room-devices', data)
}

/** 更新机房设备 */
export const updateRoomDevice = (id: number, data: RoomDeviceUpdateData) => {
  return request.put<{ message: string }>(`/DC-Control/v1/room-devices/${id}`, data)
}

/** 删除机房设备 */
export const deleteRoomDevice = (id: number) => {
  return request.delete<{ message: string }>(`/DC-Control/v1/room-devices/${id}`)
}

/** 更新采集配置 */
export const patchRoomDeviceCollect = (id: number, data: RoomDeviceCollectData) => {
  return request.patch<{ message: string }>(`/DC-Control/v1/room-devices/${id}/collect`, data)
}

/** 查询最新指标 */
export const fetchLatestMetrics = (serviceIdentity: string) => {
  return request.get<RoomDeviceMetricsLatestResponse>('/DC-Control/v1/room-devices/metrics/latest', {
    params: { service_identity: serviceIdentity },
  })
}

/** 上报指标（匿名接口） */
export const uploadMetrics = (data: MetricsUploadData) => {
  return request.post<{ message: string }>('/DC-Control/v1/room-devices/metrics/upload', data)
}

/** 设备分类选项 */
export const DEVICE_CATEGORY_OPTIONS = [
  { label: '电力', value: 'power' },
  { label: 'UPS', value: 'ups' },
  { label: '空调', value: 'air_conditioner' },
  { label: '温湿度', value: 'temp_humidity' },
  { label: '消防', value: 'fire_control' },
  { label: '门禁', value: 'access_control' },
  { label: '漏水', value: 'water_leak' },
  { label: '有害气体', value: 'hazardous_gas' },
]

/** 设备分类映射 */
export const DEVICE_CATEGORY_MAP: Record<string, string> = {
  power: '电力',
  ups: 'UPS',
  air_conditioner: '空调',
  temp_humidity: '温湿度',
  fire_control: '消防',
  access_control: '门禁',
  water_leak: '漏水',
  hazardous_gas: '有害气体',
}
