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

/** 安全设备服务项 */
export interface SecurityServiceItem {
  id: number
  created_at: string
  updated_at: string
  deleted_at: string | null
  service_identity: string
  oid: string
  server_identity: string
  name: string
  category: string
  type: string
  description: string
  enabled: boolean
  interval: number
  extra: string
  tags: string
  status_url: string
  agent_config: string
  collect_on: boolean
  collect_args: string
  collect_interval: number
  collect_last_result: string
  status: string
  status_code: number
  status_message: string
  response_time: number
  last_check_time: string
  last_online_time: string | null
  last_offline_time: string | null
  continuous_errors: number
  uptime: number
}

/** 安全设备服务列表数据 */
export interface SecurityServiceListData {
  total: number
  page: number
  page_size: number
  data: SecurityServiceItem[]
}

/** 安全设备服务列表响应参数 */
export interface SecurityServiceListParams {
  page?: number
  size?: number
  keyword?: string
  enabled?: boolean
}

/** 创建/更新安全设备服务请求参数 */
export interface SecurityServiceFormData {
  service_identity?: string
  oid?: string
  server_identity?: string
  name?: string
  category?: string
  type?: string
  description?: string
  enabled?: boolean
  interval?: number
  extra?: string
  tags?: string
  status_url?: string
  agent_config?: string
  collect_on?: boolean
  collect_args?: string
  collect_interval?: number
  policy_ids?: number[]
}

/** 安全设备指标项 */
export interface SecurityMetric {
  metric_name: string
  metric_value: number
  metric_unit: string
}

/** 安全设备最新指标数据 */
export interface SecurityMetricsLatestData {
  service_identity: string
  latest_timestamp: string | null
  count: number
  metrics: SecurityMetric[]
}

/** 获取安全设备服务列表（分页） */
export const fetchSecurityServiceList = (params?: SecurityServiceListParams) => {
  return request.get<{ code: number; message: string; details: SecurityServiceListData }>('/DC-Control/v1/security', { params })
}

/** 获取安全设备服务详情 */
export const fetchSecurityServiceDetail = (id: number) => {
  return request.get<{ code: number; message: string; details: SecurityServiceItem }>(`/DC-Control/v1/security/${id}`)
}

/** 创建安全设备服务 */
export const createSecurityService = (data: SecurityServiceFormData) => {
  return request.post<{ code: number; message: string; details: { message: string; id: number } }>('/DC-Control/v1/security', data)
}

/** 更新安全设备服务 */
export const updateSecurityService = (id: number, data: Partial<SecurityServiceFormData>) => {
  return request.put<{ code: number; message: string; details: { message: string } }>(`/DC-Control/v1/security/${id}`, data)
}

/** 删除安全设备服务 */
export const deleteSecurityService = (id: number) => {
  return request.delete<{ code: number; message: string; details: { message: string } }>(`/DC-Control/v1/security/${id}`)
}

/** 获取最新时序指标 */
export const fetchSecurityMetricsLatest = (serviceIdentity: string) => {
  return request.get<{ code: number; message: string; details: SecurityMetricsLatestData }>('/DC-Control/v1/security/metrics/latest', {
    params: { service_identity: serviceIdentity },
  })
}
