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

/** 服务器类型 */
export interface ServerItem {
  id: number
  created_at: string
  updated_at: string
  deleted_at: string | null
  server_identity: string
  name: string
  host: string
  ip_address: string
  description: string
  os: string
  os_version: string
  kernel: string
  server_type: string
  tags: string
  location: string
  remote_access: string
  remote_port: number
  agent_config: string
  status: string
  last_check_time: string
  collect_on: boolean
  collect_args: string
  collect_interval: number
  collect_last_result: string
  is_ip_scan_server: boolean
  ip_scan_port: number
}

/** 服务器列表响应 */
export interface ServerListResponse {
  total: number
  page: number
  page_size: number
  data: ServerItem[]
}

/** 服务器列表请求参数 */
export interface ServerListParams {
  page?: number
  size?: number
  keyword?: string
  collect_on?: boolean
  is_ip_scan_server?: boolean
}

/** 创建/更新服务器请求参数 */
export interface ServerFormData {
  server_identity?: string
  name?: string
  host?: string
  ip_address?: string
  description?: string
  os?: string
  os_version?: string
  kernel?: string
  server_type?: string
  tags?: string
  location?: string
  remote_access?: string
  remote_port?: number
  agent_config?: string
  status?: string
  last_check_time?: string
  collect_on?: boolean
  collect_args?: string
  collect_interval?: number
  collect_last_result?: string
  is_ip_scan_server?: boolean
  ip_scan_port?: number
}

/** 获取服务器列表（分页） */
export const fetchServerList = (params?: ServerListParams) => {
  return request.get<ServerListResponse>('/DC-Control/v1/servers', { params })
}

/** 获取服务器详情 */
export const fetchServerDetail = (id: number) => {
  return request.get<ServerItem>(`/DC-Control/v1/servers/${id}`)
}

/** 创建服务器 */
export const createServer = (data: ServerFormData) => {
  return request.post<ServerItem>('/DC-Control/v1/servers', data)
}

/** 更新服务器 */
export const updateServer = (id: number, data: Partial<ServerFormData>) => {
  return request.put<{ message: string }>(`/DC-Control/v1/servers/${id}`, data)
}

/** 删除服务器 */
export const deleteServer = (id: number) => {
  return request.delete<{ message: string }>(`/DC-Control/v1/servers/${id}`)
}

/** 主机最新指标摘要（统计卡片，与 dc-control /servers/metrics/summary 一致） */
export interface HostMetricsUseStat {
  total_bytes: number
  used_bytes: number
  free_bytes: number
  used_percent: number
}

export interface HostMetricsDiskMount {
  mount: string
  total_bytes: number
  used_bytes: number
  free_bytes: number
  used_percent: number
}

export interface HostMetricsCpuCard {
  usage_percent: number
  logical_cores_total: number
}

export interface HostMetricsSummary {
  server_identity: string
  timestamp?: string
  has_data: boolean
  memory?: HostMetricsUseStat
  disk_root?: HostMetricsDiskMount
  data_disks?: HostMetricsDiskMount[]
  cpu?: HostMetricsCpuCard
}

export interface HostNetworkTrafficPoint {
  time: string
  recv_mbps: number
  send_mbps: number
}

export interface HostNetworkTrafficPayload {
  server_identity: string
  hours: number
  /** 时间窗内是否有主机采集写入的网络累计指标行 */
  has_data: boolean
  /** 是否已根据相邻采样差分算出速率曲线（至少两个采样点） */
  has_rate_series?: boolean
  points: HostNetworkTrafficPoint[]
  note: string
}

/** 最新一批主机指标（用于监控大屏卡片） */
export const fetchServerMetricsSummary = (serverIdentity: string) => {
  return request.get<{ code: number; details?: HostMetricsSummary; message?: string }>(
    '/DC-Control/v1/servers/metrics/summary',
    { params: { server_identity: serverIdentity } },
  )
}

/** 近 N 小时网络收/发速率（Mbps，相邻采样字节差分） */
export const fetchServerNetworkTraffic = (serverIdentity: string, hours = 6) => {
  return request.get<{ code: number; details?: HostNetworkTrafficPayload; message?: string }>(
    '/DC-Control/v1/servers/metrics/network-traffic',
    { params: { server_identity: serverIdentity, hours } },
  )
}
