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

export interface TrafficListItem {
  id: number
  topology_id: number
  link_id: number
  node_id: string
  time_granularity: string
  time_point: string
  in_bytes: number
  out_bytes: number
  total_bytes: number
  in_packets: number
  out_packets: number
  total_packets: number
  connections: number
  avg_latency: number
  packet_loss: number
  bandwidth: number
  bandwidth_peak: number
  protocol_stats?: string
  top_sources?: string
}

export interface TrafficListResponse {
  total: number
  page: number
  page_size: number
  data: TrafficListItem[]
}

export interface TrafficSummary {
  total_in_bytes: number
  total_out_bytes: number
  total_bytes: number
  avg_bandwidth: number
  peak_bandwidth: number
  total_connections: number
  avg_packet_loss: number
}

export interface TrafficTrendPoint {
  time: string
  in_bytes: number
  out_bytes: number
  total_bytes: number
}

export interface TrafficTopItem {
  node_id: string
  total_in_bytes: number
  total_out_bytes: number
  total_bytes: number
  total_packets: number
}

export interface TrafficDashboardResponse {
  direction: {
    inbound_percent: number
    outbound_percent: number
  }
  protocols: Array<{
    name: string
    percent: number
  }>
  top_applications: Array<{
    name: string
    in_bytes: number
    out_bytes: number
    total_bytes: number
    share_percent: number
    sessions: number
    port: string
  }>
  top_sources: Array<{
    ip: string
    name: string
    total_bytes: number
    total_packets: number
  }>
  bandwidth: Array<{
    name: string
    percent: number
  }>
}

export const fetchTrafficList = (params?: Record<string, any>) =>
  request.get<{ code: number; details?: TrafficListResponse; message?: string }>('/DC-Control/v1/traffic', {
    params,
  })

export const fetchTrafficSummary = (params?: Record<string, any>) =>
  request.get<{ code: number; details?: TrafficSummary; message?: string }>('/DC-Control/v1/traffic/summary', { params })

export const fetchTrafficTrend = (params?: Record<string, any>) =>
  request.get<{ code: number; details?: { data?: TrafficTrendPoint[] }; message?: string }>('/DC-Control/v1/traffic/trend', { params })

export const fetchTopTraffic = (params?: Record<string, any>) =>
  request.get<{ code: number; details?: { data?: TrafficTopItem[] }; message?: string }>('/DC-Control/v1/traffic/top', { params })

export const fetchRealtimeTraffic = (params?: Record<string, any>) =>
  request.get<{ code: number; details?: { bandwidth?: number; connections?: number }; message?: string }>(
    '/DC-Control/v1/traffic/realtime',
    { params }
  )

export const fetchTrafficDashboard = (params?: Record<string, any>) =>
  request.get<{ code: number; details?: TrafficDashboardResponse; message?: string }>('/DC-Control/v1/traffic/dashboard', { params })
