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

/** 存储设备服务项 */
export interface StorageItem {
  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 StorageListResponse {
  total: number
  page: number
  page_size: number
  data: StorageItem[]
}

/** 存储设备列表请求参数 */
export interface StorageListParams {
  page?: number
  size?: number
  keyword?: string
  enabled?: boolean
}

/** 创建存储设备请求参数 */
export interface StorageCreateData {
  service_identity: string
  name: string
  category?: string
  type: string
  oid?: string
  server_identity?: 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 StorageUpdateData {
  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
  policy_ids?: number[]
}

/** 指标项 */
export interface StorageMetricItem {
  metric_name: string
  metric_value: number
  metric_unit: string
}

/** 最新指标响应 */
export interface StorageMetricsLatestResponse {
  service_identity: string
  latest_timestamp: string | null
  count: number
  metrics: StorageMetricItem[]
}

/** 获取存储设备服务列表（匿名接口） */
export const fetchStorageList = (params?: StorageListParams) => {
  return request.get<StorageListResponse>('/DC-Control/v1/storage', { params })
}

/** 获取存储设备服务详情 */
export const fetchStorageDetail = (id: number) => {
  return request.get<StorageItem>(`/DC-Control/v1/storage/${id}`)
}

/** 创建存储设备服务 */
export const createStorage = (data: StorageCreateData) => {
  return request.post<{ message: string; id: number }>('/DC-Control/v1/storage', data)
}

/** 更新存储设备服务 */
export const updateStorage = (id: number, data: StorageUpdateData) => {
  return request.put<{ message: string }>(`/DC-Control/v1/storage/${id}`, data)
}

/** 删除存储设备服务 */
export const deleteStorage = (id: number) => {
  return request.delete<{ message: string }>(`/DC-Control/v1/storage/${id}`)
}

/** 查询最新一批时序指标 */
export const fetchStorageMetricsLatest = (serviceIdentity: string) => {
  return request.get<StorageMetricsLatestResponse>('/DC-Control/v1/storage/metrics/latest', {
    params: { service_identity: serviceIdentity },
  })
}
