export const AUDIT_ACTIONS = [
  { value: 'THRESHOLD_CREATED', label: 'Threshold created' },
  { value: 'THRESHOLD_UPDATED', label: 'Threshold updated' },
  { value: 'THRESHOLD_DELETED', label: 'Threshold deleted' },
  { value: 'ALERT_ACKNOWLEDGED', label: 'Alert acknowledged' },
  { value: 'ALERT_RESOLVED', label: 'Alert resolved' },
  { value: 'ENCOUNTER_STATUS_CHANGED', label: 'Encounter status changed' },
  { value: 'PATIENT_REGISTERED', label: 'Patient registered' },
  { value: 'PATIENT_UPDATED', label: 'Patient updated' },
  { value: 'SUPPRESSION_WINDOW_SET', label: 'Suppression window set' },
  { value: 'USER_LOGIN', label: 'User login' },
  { value: 'AUTHORIZATION_DENIED', label: 'Authorization denied' },
]

export const PHI_ACCESS_TYPES = [
  { value: 'VIEW', label: 'View' },
  { value: 'LIST', label: 'List' },
  { value: 'SEARCH', label: 'Search' },
  { value: 'CREATE', label: 'Create' },
  { value: 'UPDATE', label: 'Update' },
]

export function actionLabel(action) {
  return AUDIT_ACTIONS.find((a) => a.value === action)?.label ?? action
}

export function accessTypeLabel(accessType) {
  return PHI_ACCESS_TYPES.find((a) => a.value === accessType)?.label ?? accessType
}

export function formatAuditTimestamp(iso) {
  if (!iso) return '—'
  return new Date(iso).toLocaleString([], {
    month: 'short',
    day: 'numeric',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
  })
}

export function formatJsonBlock(raw) {
  if (!raw) return null
  try {
    return JSON.stringify(JSON.parse(raw), null, 2)
  } catch {
    return raw
  }
}

export function defaultFromDate() {
  const d = new Date()
  d.setDate(d.getDate() - 30)
  return d.toISOString().slice(0, 16)
}

export function defaultToDate() {
  return new Date().toISOString().slice(0, 16)
}

export function toIsoOffset(localDatetime) {
  if (!localDatetime) return undefined
  return new Date(localDatetime).toISOString()
}
