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

export type NetworkDeviceProtocol = 'snmp' | 'ssh' | 'telnet'
export type NetworkDeviceSnmpVersion = 'v1' | 'v2c' | 'v3'
export type NetworkDeviceStatus = 'online' | 'offline' | 'error' | 'unknown'

export interface NetworkDeviceService {
  id: number
  created_at: string
  updated_at: string
  deleted_at: string | null
  service_identity: string
  server_identity: string
  name: string
  category: string
  type: string
  host: string
  port: number
  protocol: NetworkDeviceProtocol
  community: string
  snmp_version: NetworkDeviceSnmpVersion
  username: string
  vendor: string
  model: string
  location: string
  description: string
  enabled: boolean
  interval: number
  extra: string
  tags: string
  status: NetworkDeviceStatus
  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 NetworkDeviceListResponse {
  total: number
  page: number
  page_size: number
  data: NetworkDeviceService[]
}

export interface NetworkDeviceQueryParams {
  page?: number
  size?: number
  keyword?: string
  enabled?: boolean
}

export interface CreateNetworkDeviceParams {
  service_identity?: string
  server_identity?: string
  name: string
  category: string
  type?: string
  host: string
  port?: number
  protocol?: NetworkDeviceProtocol
  community?: string
  snmp_version?: NetworkDeviceSnmpVersion
  username?: string
  password?: string
  vendor?: string
  model?: string
  location?: string
  description?: string
  enabled?: boolean
  interval?: number
  extra?: string
  tags?: string
}

export type UpdateNetworkDeviceParams = Partial<CreateNetworkDeviceParams>

export const fetchNetworkDeviceList = (params: NetworkDeviceQueryParams) =>
  request.get<NetworkDeviceListResponse>('/DC-Control/v1/network-device-services', { params })

export const fetchNetworkDeviceDetail = (id: number) => request.get<NetworkDeviceService>(`/DC-Control/v1/network-device-services/${id}`)

export const createNetworkDevice = (data: CreateNetworkDeviceParams) =>
  request.post<{ message: string; id: number }>('/DC-Control/v1/network-device-services', data)

export const updateNetworkDevice = (id: number, data: UpdateNetworkDeviceParams) =>
  request.put<{ message: string }>(`/DC-Control/v1/network-device-services/${id}`, data)

export const deleteNetworkDevice = (id: number) => request.delete<{ message: string }>(`/DC-Control/v1/network-device-services/${id}`)
