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

/** IP 扫描任务（与 dc-control ControlIpScan 对齐） */
export interface IpScanTask {
  id: number
  name: string
  type: string
  description?: string
  target_range: string
  port_range?: string
  config?: string
  timeout?: number
  concurrency?: number
  cron_expr?: string
  status?: string
  server_id: number
  last_scan_time?: string
  next_scan_time?: string
  last_scan_status?: string
  total_count?: number
  online_count?: number
  scan_count?: number
  enable?: boolean
  priority?: number
  created_at?: string
  updated_at?: string
}

export interface IpScanListResponse {
  total: number
  page: number
  page_size: number
  data: IpScanTask[]
}

/** 扫描任务分页列表 */
export const fetchIpScanList = (params?: { page?: number; size?: number; keyword?: string }) =>
  request.get<{ code: number; details?: IpScanListResponse; message?: string }>('/DC-Control/v1/ipscans', {
    params,
  })

export const fetchIpScanDetail = (id: number) =>
  request.get<{ code: number; details?: IpScanTask; message?: string }>(`/DC-Control/v1/ipscans/${id}`)

/** 触发一次扫描（会创建 scan_run 并调用 Agent）；成功体见文档 { message: "scan triggered" } */
export const triggerIpScan = (id: number) =>
  request.post<{ code: number; details?: { message?: string }; message?: string }>(
    `/DC-Control/v1/ipscans/${id}/trigger`,
  )

/** 启动扫描任务（文档：将 status 置为 running，与 Cron 调度语义相关） */
export const startIpScan = (id: number) =>
  request.post<{ code: number; details?: { message?: string }; message?: string }>(
    `/DC-Control/v1/ipscans/${id}/start`,
  )

/** 停止扫描任务（文档：将 status 置为 stopped） */
export const stopIpScan = (id: number) =>
  request.post<{ code: number; details?: { message?: string }; message?: string }>(
    `/DC-Control/v1/ipscans/${id}/stop`,
  )

/** 创建扫描任务 */
export const createIpScan = (data: Partial<IpScanTask>) =>
  request.post<{ code: number; details?: IpScanTask; message?: string }>('/DC-Control/v1/ipscans', data)

/** 更新扫描任务 */
export const updateIpScan = (id: number, data: Partial<IpScanTask>) =>
  request.put<{ code: number; message?: string }>(`/DC-Control/v1/ipscans/${id}`, data)

/** 删除扫描任务 */
export const deleteIpScan = (id: number) =>
  request.delete<{ code: number; message?: string }>(`/DC-Control/v1/ipscans/${id}`)
