import { api } from './client'

/** Maps frontend pill values to backend PascalCase enum strings. */
export const FEEDBACK_TYPE_MAP = {
  'useful': 'Useful',
  'too-early': 'TooEarly',
  'too-late': 'TooLate',
  'false-positive': 'FalsePositive',
  'missing-context': 'MissingContext',
  'would-act': 'WouldAct',
}

export function submitAlertFeedback(alertId, rating, comment = '') {
  const feedbackType = FEEDBACK_TYPE_MAP[rating]
  if (!feedbackType) throw new Error(`Unknown feedback rating: ${rating}`)
  return api.post(`/api/v1/alerts/${alertId}/feedback`, {
    feedbackType,
    comment: comment || null,
  })
}

export function fetchQualityMetricsSummary(from, to) {
  const params = new URLSearchParams()
  if (from) params.set('from', from)
  if (to) params.set('to', to)
  const qs = params.toString()
  return api.get(`/api/v1/alerts/quality-metrics/summary${qs ? `?${qs}` : ''}`)
}

export function fetchQualityMetrics({ alertType, from, to } = {}) {
  const params = new URLSearchParams()
  if (alertType) params.set('alertType', alertType)
  if (from) params.set('from', from)
  if (to) params.set('to', to)
  const qs = params.toString()
  return api.get(`/api/v1/alerts/quality-metrics${qs ? `?${qs}` : ''}`)
}