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

export interface LicenceQuotas {
  max_database: number
  max_middleware: number
  max_network_device: number
  max_security: number
  max_storage: number
  max_pc: number
  max_server: number
  max_user: number
  max_role: number
  max_permission: number
  max_menu: number
}

export interface LicenceInfo {
  id: string
  platform_name: string
  workspace: string
  issued_on: string
  valid_from: string
  expires_on: string
  quotas: LicenceQuotas
}

/** 获取 采集器 */
export const fetchCollectors = (data: { page: number; size: number; keyword?: string }) =>
  request.get('/DC-Control/v1/collectors', { params: data })

/** 新增 采集器 */
export const createCollector = (data: any) => request.post('/DC-Control/v1/collectors', data)

/** 删除 采集器 */
export const deleteCollector = (id: number) => request.delete(`/DC-Control/v1/collectors/${id}`)

/** 获取 采集器详情 */
export const fetchCollectorDetail = (id: number) => request.get(`/DC-Control/v1/collectors/${id}`)

/** 更新 采集器 */
export const updateCollector = (data: any) => request.put(`/DC-Control/v1/collectors/${data.id}`, data)

/** 获取 采集器统计数据 */
export const fetchCollectorStatistics = () => request.get('/DC-Control/v1/statistics')

/** 获取 许可证信息 */
export const fetchLicenseInfo = () =>
  request.get<{ code: number; details: LicenceInfo; message: string; timeseq: number }>('/DC-Control/v1/license')

export interface PageResult<T> {
  total: number
  page: number
  page_size: number
  data: T[]
}

export interface OptionItem {
  id?: number
  label: string
  value: string
}

export interface ControlResource {
  id: number
  resource_uid: string
  resource_category: string
  service_identity: string
  display_name: string
  description?: string
  status: string
  business_system_id?: number
  owner_user_id?: number
  department_id?: number
  asset_id?: number
  datacenter_id?: number
  room_id?: number
  rack_id?: number
  unit_start?: number
  unit_end?: number
  location_ref?: string
  tags?: string
  source_service?: string
  source_table?: string
  source_id?: number
  created_at?: string
  updated_at?: string
}

export interface ControlResourcePayload {
  resource_category?: string
  service_identity?: string
  display_name?: string
  description?: string
  status?: string
  business_system_id?: number | null
  owner_user_id?: number | null
  department_id?: number | null
  asset_id?: number | null
  datacenter_id?: number | null
  room_id?: number | null
  rack_id?: number | null
  unit_start?: number | null
  unit_end?: number | null
  location_ref?: string | null
  tags?: string
}

export interface ControlResourceType {
  id: number
  resource_category: string
  display_name: string
  metric_template?: string
  supported_collect_method?: string
  description?: string
  enabled: boolean
}

export interface ControlMetricDefinition {
  id: number
  metric_code: string
  metric_name: string
  resource_category: string
  unit?: string
  threshold_suggestion?: string
  collect_method?: string
  description?: string
  enabled: boolean
}

export interface ControlMetricSeries {
  id: number
  resource_uid: string
  metric_code: string
  collection_task_id?: string
  storage_driver: string
  database_name?: string
  table_name?: string
  series_key?: string
  retention_policy?: string
  enabled: boolean
}

export interface ControlBusinessSystem {
  id: number
  code: string
  name: string
  system_type: string
  owner_user_id?: number
  department_id?: number
  description?: string
  status: string
  is_builtin: boolean
}

export interface ControlCollectionStatus {
  id: number
  resource_uid: string
  resource_category: string
  service_identity: string
  collect_method: string
  schedule_interval: number
  retry_policy?: string
  last_status: string
  last_error?: string
  last_sample_time?: string
  trace_id?: string
  source_service?: string
  created_at?: string
  updated_at?: string
}

export interface ResourceListParams {
  page?: number
  size?: number
  keyword?: string
  resource_category?: string
  business_system_id?: number
}

export interface MetricSeriesListParams {
  page?: number
  size?: number
  keyword?: string
  resource_uid?: string
}

export const fetchControlResources = (params?: ResourceListParams) =>
  request.get<{ code?: number; details?: PageResult<ControlResource>; message?: string }>('/DC-Control/v1/resources', { params })

export const fetchControlResourceOptions = (params?: { resource_category?: string }) =>
  request.get<{ code?: number; details?: { list: OptionItem[]; count: number }; message?: string }>('/DC-Control/v1/resources/options', {
    params,
  })

export const createControlResource = (data: ControlResourcePayload) =>
  request.post<{ code?: number; details?: ControlResource; message?: string }>('/DC-Control/v1/resources', data)

export const updateControlResource = (id: number, data: ControlResourcePayload) =>
  request.put<{ code?: number; details?: ControlResource; message?: string }>(`/DC-Control/v1/resources/${id}`, data)

export const deleteControlResource = (id: number) =>
  request.delete<{ code?: number; details?: { message: string }; message?: string }>(`/DC-Control/v1/resources/${id}`)

export const backfillControlResources = () =>
  request.post<{ code?: number; details?: { message: string }; message?: string }>('/DC-Control/v1/resources/backfill')

export const fetchControlResourceTypes = (params?: { page?: number; size?: number; keyword?: string }) =>
  request.get<{ code?: number; details?: PageResult<ControlResourceType>; message?: string }>('/DC-Control/v1/resource-types', { params })

export const fetchControlResourceTypeOptions = () =>
  request.get<{ code?: number; details?: { list: OptionItem[]; count: number }; message?: string }>('/DC-Control/v1/resource-types/options')

export const fetchControlMetricDefinitions = (params?: { page?: number; size?: number; keyword?: string; resource_category?: string }) =>
  request.get<{ code?: number; details?: PageResult<ControlMetricDefinition>; message?: string }>('/DC-Control/v1/metric-definitions', {
    params,
  })

export const fetchControlMetricDefinitionOptions = (params?: { resource_category?: string }) =>
  request.get<{ code?: number; details?: { list: OptionItem[]; count: number }; message?: string }>(
    '/DC-Control/v1/metric-definitions/options',
    { params }
  )

export const fetchControlMetricSeries = (params?: MetricSeriesListParams) =>
  request.get<{ code?: number; details?: PageResult<ControlMetricSeries>; message?: string }>('/DC-Control/v1/metric-series', { params })

export const fetchControlBusinessSystems = (params?: { page?: number; size?: number; keyword?: string; status?: string }) =>
  request.get<{ code?: number; details?: PageResult<ControlBusinessSystem>; message?: string }>('/DC-Control/v1/business-systems', {
    params,
  })

export const fetchControlBusinessSystemOptions = () =>
  request.get<{ code?: number; details?: { list: OptionItem[]; count: number }; message?: string }>(
    '/DC-Control/v1/business-systems/options'
  )

export const fetchCollectionStatus = (params?: {
  page?: number
  size?: number
  keyword?: string
  resource_category?: string
  resource_uid?: string
  status?: string
}) =>
  request.get<{ code?: number; details?: PageResult<ControlCollectionStatus>; message?: string }>('/DC-Control/v1/collection/status', {
    params,
  })
