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

// ==================== 类型定义 ====================

/** 拓扑对象 */
export interface Topology {
  id: number
  name: string
  type: 'layer2' | 'layer3' | 'physical'
  description?: string
  group_id?: number
  group_name?: string
  node_data?: string
  collector_id?: number
  status?: 'pending' | 'discovering' | 'active' | 'error'
  discovery_config?: string
  last_discovery?: string
  next_discovery?: string
  node_count?: number
  link_count?: number
  enable?: boolean
  created_at?: string
  updated_at?: string
}

/** 链路对象 */
export interface Link {
  id?: number
  topology_id?: number
  source: string
  target: string
  type?: 'physical' | 'virtual'
  label?: string
  bandwidth?: number
  latency?: number
  packet_loss?: number
  status?: 'up' | 'down' | 'unknown'
  in_traffic?: number
  out_traffic?: number
  metadata?: string
  description?: string
  created_at?: string
  updated_at?: string
}

/** 拓扑分组 */
export interface TopologyGroup {
  id: number
  name: string
  parent_id?: number
  level?: number
  path?: string
  description?: string
  sort?: number
  enable?: boolean
  topology_count?: number
  total_link_count?: number
  total_topology_count?: number
  children?: TopologyGroup[]
  created_at?: string
  updated_at?: string
}

/** 拓扑节点对象 */
export interface TopologyNode {
  id: string
  topology_id: number
  label: string
  type: string
  ip?: string
  status?: string
  alerts?: number
  traffic?: string
  description?: string
  parentId?: string | null
  level?: number
  position?: { x: number; y: number }
  /** 设计 v2.0：绑定资产 / 子拓扑 */
  ref_type?: string
  ref_id?: number
  sub_topology_id?: number | null
  created_at?: string
  updated_at?: string
}

// ==================== 分组管理接口 ====================

/** 获取分组列表 */
export const fetchTopologyGroups = (data?: { 
  parent_id?: number; 
  page?: number; 
  size?: number; 
  keyword?: string; 
  enable?: boolean 
}) =>
  request.get('/DC-Control/v1/topology-groups', { params: data })

/** 创建分组 */
export const createTopologyGroup = (data: Partial<TopologyGroup>) =>
  request.post('/DC-Control/v1/topology-groups', data)

/** 更新分组 */
export const updateTopologyGroup = (data: Partial<TopologyGroup> & { id: number }) =>
  request.put(`/DC-Control/v1/topology-groups/${data.id}`, data)

/** 删除分组 */
export const deleteTopologyGroup = (id: number) =>
  request.delete(`/DC-Control/v1/topology-groups/${id}`)

// ==================== 拓扑管理接口 ====================

/** 获取拓扑列表 */
export const fetchTopologies = (data: { page: number; size: number; keyword?: string; group_id?: number }) =>
  request.get('/DC-Control/v1/topologies', { params: data })

/** 获取拓扑详情 */
export const fetchTopologyDetail = (id: number) =>
  request.get(`/DC-Control/v1/topologies/${id}`)

/** 创建拓扑 */
export const createTopology = (data: Partial<Topology>) =>
  request.post('/DC-Control/v1/topologies', data)

/** 更新拓扑 */
export const updateTopology = (data: Partial<Topology> & { id: number }) =>
  request.put(`/DC-Control/v1/topologies/${data.id}`, data)

/** 删除拓扑 */
export const deleteTopology = (id: number) =>
  request.delete(`/DC-Control/v1/topologies/${id}`)

/** 触发拓扑发现 */
export const discoverTopology = (id: number) =>
  request.post(`/DC-Control/v1/topologies/${id}/discover`)

/** 获取拓扑视图（包含拓扑和链路） */
export const fetchTopologyView = (id: number) =>
  request.get(`/DC-Control/v1/topologies/${id}/view`)

/** 获取拓扑图数据（用于前端可视化） */
export const fetchTopologyGraph = (id: number) =>
  request.get(`/DC-Control/v1/topologies/${id}/graph`)

// ==================== 节点管理接口 ====================

/** 创建节点 */
export const createNode = (topologyId: number, data: Partial<TopologyNode>) =>
  request.post(`/DC-Control/v1/topologies/${topologyId}/nodes`, data)

/** 更新节点 */
export const updateNode = (topologyId: number, nodeId: string, data: Partial<TopologyNode>) =>
  request.put(`/DC-Control/v1/topologies/${topologyId}/nodes/${nodeId}`, data)

/** 删除节点 */
export const deleteNode = (topologyId: number, nodeId: string) =>
  request.delete(`/DC-Control/v1/topologies/${topologyId}/nodes/${nodeId}`)

/** 批量更新节点位置 */
export const updateNodesPositions = (topologyId: number, positions: Array<{ id: string; position: { x: number; y: number } }>) =>
  request.put(`/DC-Control/v1/topologies/${topologyId}/nodes/positions`, { positions })

/** 按资产 ID 批量导入节点（后端生成 node_id=asset-{id} 与 ref_type=asset） */
export const batchImportAssetNodes = (topologyId: number, asset_ids: number[]) =>
  request.post<{ code: number; details?: { message: string; imported: number } }>(
    `/DC-Control/v1/topologies/${topologyId}/nodes/batch-import`,
    { asset_ids },
  )

// ==================== 链路管理接口 ====================

/** 获取链路列表 */
export const fetchLinks = (topologyId: number) =>
  request.get(`/DC-Control/v1/topologies/${topologyId}/links`)

/** 创建链路 */
export const createLink = (topologyId: number, data: Partial<Link>) =>
  request.post(`/DC-Control/v1/topologies/${topologyId}/links`, data)

/** 更新链路 */
export const updateLink = (topologyId: number, linkId: number, data: Partial<Link>) =>
  request.put(`/DC-Control/v1/topologies/${topologyId}/links/${linkId}`, data)

/** 删除链路 */
export const deleteLink = (topologyId: number, linkId: string) =>
  request.delete(`/DC-Control/v1/topologies/${topologyId}/links/${linkId}`)
