import { apiClient } from './client'

export interface AlertCreate {
  name: string
  alert_type: string
  condition: Record<string, any>
}

export interface AlertUpdate {
  name?: string
  condition?: Record<string, any>
  enabled?: boolean
}

export interface AlertResponse {
  id: number
  name: string
  alert_type: string
  condition: Record<string, any>
  enabled: boolean
  triggered: boolean
  triggered_at: string | null
  created_at: string
  updated_at: string
}

export const alertsApi = {
  listAlerts: async (enabledOnly: boolean = false): Promise<AlertResponse[]> => {
    const response = await apiClient.get('/api/alerts/', {
      params: { enabled_only: enabledOnly },
    })
    return response.data
  },

  getAlert: async (alertId: number): Promise<AlertResponse> => {
    const response = await apiClient.get(`/api/alerts/${alertId}`)
    return response.data
  },

  createAlert: async (alert: AlertCreate): Promise<AlertResponse> => {
    const response = await apiClient.post('/api/alerts/', alert)
    return response.data
  },

  updateAlert: async (alertId: number, updates: AlertUpdate): Promise<AlertResponse> => {
    const response = await apiClient.put(`/api/alerts/${alertId}`, updates)
    return response.data
  },

  deleteAlert: async (alertId: number): Promise<void> => {
    await apiClient.delete(`/api/alerts/${alertId}`)
  },
}
