const circleLabel = (c) => c ? c.charAt(0).toUpperCase() + c.slice(1) : c

const formatters = {
  member_joined: (m) => ({
    text: 'Joined Ghost Guild',
    icon: 'i-lucide-star'
  }),
  event_registered: (m) => ({
    text: `Registered for ${m.eventTitle || 'an event'}`,
    icon: 'i-lucide-calendar',
    link: m.eventSlug ? `/events/${m.eventSlug}` : null,
    linkText: m.eventTitle
  }),
  event_cancelled: (m) => ({
    text: `Cancelled registration for ${m.eventTitle || 'an event'}`,
    icon: 'i-lucide-calendar-x',
    link: m.eventSlug ? `/events/${m.eventSlug}` : null,
    linkText: m.eventTitle
  }),
  event_waitlisted: (m) => ({
    text: `Joined waitlist for ${m.eventTitle || 'an event'}`,
    icon: 'i-lucide-calendar-clock',
    link: null,
    linkText: null
  }),
  peer_support_enabled: (m) => ({
    text: m.topics?.length
      ? `Enabled peer support (${m.topics.join(', ')})`
      : 'Enabled peer support',
    icon: 'i-lucide-users'
  }),
  peer_support_disabled: () => ({
    text: 'Disabled peer support',
    icon: 'i-lucide-users'
  }),
  circle_changed: (m) => ({
    text: `Changed circle from ${circleLabel(m.from)} to ${circleLabel(m.to)}`,
    icon: 'i-lucide-refresh-cw'
  }),
  contribution_changed: (m) => ({
    text: `Changed contribution from $${m.from}/mo to $${m.to}/mo`,
    icon: 'i-lucide-coins'
  }),
  email_changed: (m) => ({
    text: `Changed email address`,
    icon: 'i-lucide-mail'
  }),
  profile_updated: (m) => ({
    text: m.fields?.length
      ? `Updated profile (${m.fields.join(', ')})`
      : 'Updated profile',
    icon: 'i-lucide-user-pen'
  }),
  subscription_created: (m) => ({
    text: m.tier ? `Started $${m.tier}/mo subscription` : 'Started subscription',
    icon: 'i-lucide-credit-card'
  }),
  subscription_cancelled: () => ({
    text: 'Cancelled subscription',
    icon: 'i-lucide-credit-card'
  }),
  status_changed: (m) => ({
    text: `Status changed from ${m.from} to ${m.to}${m.reason ? ` (${m.reason})` : ''}`,
    icon: 'i-lucide-shield'
  }),
  role_changed: (m) => ({
    text: `Role changed from ${m.from} to ${m.to}`,
    icon: 'i-lucide-shield'
  }),
  admin_profile_update: (m) => ({
    text: m.fields?.length
      ? `Profile updated by admin (${m.fields.join(', ')})`
      : 'Profile updated by admin',
    icon: 'i-lucide-user-pen'
  }),
  slack_invited: (m) => ({
    text: `Slack invitation: ${m.status || 'sent'}`,
    icon: 'i-lucide-message-square'
  }),
  email_sent: (m) => ({
    text: m.subject ? `Email: ${m.subject}` : 'Email sent',
    icon: 'i-lucide-mail',
    emailBody: m.body || null
  }),
  community_connections_updated: () => ({
    text: 'Updated community connections',
    icon: 'i-lucide-users'
  }),
  connection_requested: (m) => ({
    text: `Sent connection request to ${m.memberName || 'a member'}`,
    icon: 'i-lucide-user-plus'
  }),
  connection_confirmed: (m) => ({
    text: `Connected with ${m.memberName || 'a member'}`,
    icon: 'i-lucide-handshake'
  }),
  tag_suggested: (m) => ({
    text: `Suggested tag: ${m.label || 'unknown'}`,
    icon: 'i-lucide-tag'
  })
}

export function formatActivity(entry) {
  const formatter = formatters[entry.type]
  if (!formatter) {
    return { text: entry.type.replace(/_/g, ' '), icon: 'i-lucide-activity' }
  }
  return formatter(entry.metadata || {})
}
