export function formatRoleLabel(role) {
  const map = {
    NURSE: 'Nurse',
    PHYSICIAN: 'Physician',
    ADMIN: 'Administrator',
    INTEGRATION: 'Integration',
  }
  return map[role] ?? role?.replace(/_/g, ' ') ?? 'Clinician'
}

export function roleAcknowledgmentMessage(role) {
  const messages = {
    NURSE: 'You are acknowledging as a nurse — documenting awareness of this alert.',
    PHYSICIAN: 'You are acknowledging as a physician — confirming clinical assessment.',
    ADMIN: 'You are acknowledging as an administrator — documenting review of this alert.',
    INTEGRATION: 'You are acknowledging as an integration account.',
  }
  return messages[role] ?? 'You are acknowledging this alert in your current role.'
}

export function previewAcknowledgmentNote(role, displayName, userNote = '') {
  const roleLabel = role ?? 'UNKNOWN'
  const prefix = `[${roleLabel}] Acknowledged by ${displayName}.`
  const trimmed = userNote.trim()
  return trimmed ? `${prefix} ${trimmed}` : prefix
}

export function parseAcknowledgedBy(acknowledgedBy) {
  if (!acknowledgedBy) return { name: null, role: null }

  const match = acknowledgedBy.match(/^(.+)\s+\(([A-Z_]+)\)$/)
  if (match) {
    return {
      name: match[1].trim(),
      role: match[2],
    }
  }

  return { name: acknowledgedBy, role: null }
}

export function formatAcknowledgedByDisplay(acknowledgedBy) {
  const { name, role } = parseAcknowledgedBy(acknowledgedBy)
  if (!name) return '—'
  if (!role) return name
  return `${name} (${formatRoleLabel(role)})`
}
