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

/** 与后端 discovery/summary 对齐 */
export interface DiscoverySummary {
  device_total_current: number
  online_current: number
  offline_current: number
  other_status_current: number
  primary_scan_ids: number[]
  last_run?: DiscoveryScanRun | null
}

export interface DiscoveryScanRun {
  id: number
  scan_id: number
  run_type: string
  status: string
  started_at: string
  finished_at?: string | null
  device_total: number
  online_count: number
  offline_count: number
  new_count: number
  error_message?: string
  trigger_by: string
}

export interface DiscoveryDeviceRow {
  id: number
  ip_address: string
  scan_id?: number
  run_id?: number
  status: string
  mac_address?: string
  hostname?: string
  device_type?: string
  manufacturer?: string
  discovery_methods?: string
  last_seen_time?: string
  last_scan_time?: string
  first_seen_time?: string
}

export interface DiscoveryDevicesResponse {
  scope: string
  scan_id?: number
  primary_scan_ids?: number[]
  total: number
  page: number
  page_size: number
  data: DiscoveryDeviceRow[]
}

export interface DiscoveryScanRunsResponse {
  total: number
  page: number
  page_size: number
  data: DiscoveryScanRun[]
}

export interface DiscoveryConfig {
  id: number
  primary_scan_ids: number[]
  primary_scan_ids_raw?: string
}

export const fetchDiscoverySummary = () => request.get<{ code: number; details?: DiscoverySummary }>('/DC-Control/v1/discovery/summary')

export const fetchDiscoveryDevices = (params?: {
  scope?: 'current' | 'raw'
  scan_id?: number
  page?: number
  size?: number
  status?: string
  keyword?: string
}) =>
  request.get<{ code: number; details?: DiscoveryDevicesResponse }>('/DC-Control/v1/discovery/devices', {
    params,
  })

export const fetchDiscoveryScanRuns = (params?: { scan_id?: number; page?: number; size?: number }) =>
  request.get<{ code: number; details?: DiscoveryScanRunsResponse }>('/DC-Control/v1/discovery/scan-runs', {
    params,
  })

export const fetchDiscoveryConfig = () => request.get<{ code: number; details?: DiscoveryConfig }>('/DC-Control/v1/discovery/config')

export const updateDiscoveryConfig = (data: { primary_scan_ids: number[] }) =>
  request.put<{ code: number; details?: DiscoveryConfig }>('/DC-Control/v1/discovery/config', data)
