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

/** 数据库类型枚举 */
export type DatabaseType = 'MySQL' | 'PostgreSQL' | 'Redis' | 'MongoDB' | 'DM' | 'KingBase'

/** 数据库服务状态 */
export type DatabaseStatus = 'online' | 'offline' | 'error'

/** 数据库服务接口返回数据结构 */
export interface DatabaseService {
  id: number
  created_at: string
  updated_at: string
  deleted_at: string | null
  service_identity: string
  server_identity: string
  name: string
  category: string
  type: DatabaseType
  host: string
  port: number
  username: string
  password: string // 列表接口恒为空字符串，详情接口不返回
  database: string
  description: string
  enabled: boolean
  interval: number
  extra: string
  tags: string
  status: DatabaseStatus
  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
  collect_on: boolean
  collect_interval: number
  collect_args: string
  collect_last_result: string
}

/** 列表接口返回的分页数据 */
export interface DatabaseListResponse {
  total: number
  page: number
  page_size: number
  data: DatabaseService[]
}

/** 创建数据库服务请求参数 */
export interface CreateDatabaseParams {
  service_identity?: string
  server_identity?: string
  name: string
  category: string
  type: DatabaseType
  host: string
  port: number
  username?: string
  password?: string
  database?: string
  description?: string
  enabled?: boolean
  interval?: number
  extra?: string
  tags?: string
  collect_on?: boolean
  collect_args?: string
  collect_interval?: number
  collect_last_result?: string
}

/** 更新数据库服务请求参数 */
export interface UpdateDatabaseParams {
  service_identity?: string
  server_identity?: string
  name?: string
  category?: string
  type?: DatabaseType
  host?: string
  port?: number
  username?: string
  password?: string
  database?: string
  description?: string
  enabled?: boolean
  interval?: number
  extra?: string
  tags?: string
  collect_on?: boolean
  collect_args?: string
  collect_interval?: number
  collect_last_result?: string
}

/** 列表查询参数 */
export interface DatabaseQueryParams {
  page?: number
  size?: number
  keyword?: string
  enabled?: boolean
}

/** 指标采集请求参数 */
export interface CollectMetricsParams {
  ids: number[]
  persist?: boolean
}

/** 指标采集结果项 */
export interface CollectMetricsResultItem {
  id: number
  service_identity: string
  metrics?: Record<string, any>
  error?: string
}

/** 指标采集响应 */
export interface CollectMetricsResponse {
  results: CollectMetricsResultItem[]
}

/** 指标元数据项 */
export interface MetricMetaItem {
  metric_name: string
  metric_unit: string
  type: string
  last_timestamp: string
}

/** 指标元数据响应 */
export interface MetricMetaResponse {
  data_source: string
  collector_identity: string
  server_identity: string
  count: number
  metrics: MetricMetaItem[]
}

/** 指标元数据查询参数 */
export interface MetricMetaQueryParams {
  data_source: string
  server_identity?: string
  keyword?: string
  limit?: number
}

/**
 * 获取数据库服务列表
 * @param params 查询参数
 */
export const fetchDatabaseList = (params: DatabaseQueryParams) => request.get<DatabaseListResponse>('/DC-Control/v1/database', { params })

/**
 * 获取数据库服务详情
 * @param id 服务ID
 */
export const fetchDatabaseDetail = (id: number) => request.get<DatabaseService>(`/DC-Control/v1/database/${id}`)

/**
 * 创建数据库服务
 * @param data 创建参数
 */
export const createDatabase = (data: CreateDatabaseParams) => request.post<{ message: string; id: number }>('/DC-Control/v1/database', data)

/**
 * 更新数据库服务
 * @param id 服务ID
 * @param data 更新参数
 */
export const updateDatabase = (id: number, data: UpdateDatabaseParams) =>
  request.put<{ message: string }>(`/DC-Control/v1/database/${id}`, data)

/**
 * 删除数据库服务（软删除）
 * @param id 服务ID
 */
export const deleteDatabase = (id: number) => request.delete<{ message: string }>(`/DC-Control/v1/database/${id}`)

/**
 * 按服务主键采集指标
 * @param data 采集参数
 */
export const collectDatabaseMetrics = (data: CollectMetricsParams) =>
  request.post<CollectMetricsResponse>('/DC-Control/v1/database/metrics/collect', data)

/**
 * 查询指标元数据
 * @param params 查询参数
 */
export const fetchMetricMeta = (params: MetricMetaQueryParams) =>
  request.get<MetricMetaResponse>('/DC-Control/v1/services/metrics/meta', { params })
