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
}

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

/** 服务器列表请求参数 */
export interface ServerListParams {
  page?: number
  size?: number
  keyword?: string
  collect_on?: 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
}

/** 获取服务器列表（分页） */
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}`)
}
