import { alertTypeLabel, observationCodeLabel } from '@/api/normalize'

export const TIMELINE_EVENT_TYPES = [
  { id: 'observation', label: 'Observations' },
  { id: 'alert', label: 'Alerts' },
  { id: 'order', label: 'Orders' },
  { id: 'medication', label: 'Medications' },
  { id: 'status', label: 'Status' },
]

export function timelineEventStyles(type) {
  const styles = {
    observation: {
      dot: 'bg-blue-500',
      border: 'border-blue-500',
      badge: 'info',
    },
    alert: {
      dot: 'bg-red-500',
      border: 'border-red-500',
      badge: 'critical',
    },
    order: {
      dot: 'bg-purple-500',
      border: 'border-purple-500',
      badge: 'info',
    },
    medication: {
      dot: 'bg-green-500',
      border: 'border-green-500',
      badge: 'success',
    },
    status: {
      dot: 'bg-gray-500',
      border: 'border-gray-400',
      badge: 'info',
    },
  }
  return styles[type] ?? styles.status
}

export function formatTimelineEvent(event) {
  switch (event.type) {
    case 'observation':
      return `${observationCodeLabel(event.observationCode)} ${event.value} ${event.unit ?? ''}`.trim()
    case 'alert':
      return `${alertTypeLabel(event.alertType)} — ${event.severity} (${event.status})`
    case 'order':
      return `${event.description} — ${event.status}`
    case 'medication':
      return `${event.drugName} ${event.dose}${event.doseUnit} (${event.route})`
    case 'status':
      return event.label ?? `Status: ${event.status}`
    default:
      return event.type
  }
}

export function timelineTypeLabel(type) {
  return TIMELINE_EVENT_TYPES.find(t => t.id === type)?.label ?? type
}
