import { query } from '../../utils/db'

export default defineEventHandler(async () => {
  let rows: any[] = []
  try {
    rows = await query(`
      SELECT category, metric, warning, critical, danger, updated_at
      FROM thresholds
      ORDER BY category, metric
    `)
  } catch (e) {
    rows = []
  }
  
  const result: Record<string, Record<string, { warning: number; critical: number; danger: number }>> = {}
  
  for (const row of rows) {
    if (!result[row.category]) {
      result[row.category] = {}
    }
    result[row.category][row.metric] = {
      warning: row.warning,
      critical: row.critical,
      danger: row.danger
    }
  }
  
  return result
})
