import apiClient from './client'

export type AlertSeverity = 'info' | 'warning' | 'critical'
export type AlertSource = 'storage' | 'tape' | 'iscsi' | 'system' | 'task'

export interface Alert {
  id: string
  severity: AlertSeverity
  source: AlertSource
  title: string
  message: string
  resource_type?: string
  resource_id?: string
  is_acknowledged: boolean
  acknowledged_by?: string
  acknowledged_at?: string
  resolved_at?: string
  created_at: string
  metadata?: Record<string, any>
}

export interface AlertFilters {
  severity?: AlertSeverity
  source?: AlertSource
  is_acknowledged?: boolean
  resource_type?: string
  resource_id?: string
  limit?: number
}

export interface Metrics {
  system: {
    cpu_usage_percent: number
    memory_usage_percent: number
    memory_used_bytes: number
    memory_total_bytes: number
    disk_usage_percent: number
    uptime_seconds: number
  }
  storage: {
    total_repositories: number
    total_capacity_bytes: number
    used_capacity_bytes: number
  }
  scst: {
    total_targets: number
    total_luns: number
    active_sessions: number
  }
  tape: {
    physical_libraries: number
    physical_drives: number
    virtual_libraries: number
    virtual_drives: number
  }
  tasks: {
    total_tasks: number
    pending_tasks: number
    running_tasks: number
    completed_tasks: number
    failed_tasks: number
    avg_duration_sec: number
  }
}

export const monitoringApi = {
  listAlerts: async (filters?: AlertFilters): Promise<{ alerts: Alert[] }> => {
    const response = await apiClient.get<{ alerts: Alert[] }>('/monitoring/alerts', {
      params: filters,
    })
    return response.data
  },

  getAlert: async (id: string): Promise<Alert> => {
    const response = await apiClient.get<Alert>(`/monitoring/alerts/${id}`)
    return response.data
  },

  acknowledgeAlert: async (id: string): Promise<void> => {
    await apiClient.post(`/monitoring/alerts/${id}/acknowledge`)
  },

  resolveAlert: async (id: string): Promise<void> => {
    await apiClient.post(`/monitoring/alerts/${id}/resolve`)
  },

  getMetrics: async (): Promise<Metrics> => {
    const response = await apiClient.get<Metrics>('/monitoring/metrics')
    return response.data
  },
}

