import mongoose from 'mongoose'

const ACTIVITY_TYPES = [
  'member_joined',
  'event_registered',
  'event_cancelled',
  'event_waitlisted',
  'peer_support_enabled',
  'peer_support_disabled',
  'circle_changed',
  'contribution_changed',
  'email_changed',
  'profile_updated',
  'subscription_created',
  'subscription_cancelled',
  'status_changed',
  'role_changed',
  'admin_profile_update',
  'slack_invited',
  'email_sent',
  'community_connections_updated',
  'connection_requested',
  'connection_confirmed',
  'tag_suggested'
]

const activityLogSchema = new mongoose.Schema({
  member: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Member',
    required: true
  },
  type: {
    type: String,
    enum: ACTIVITY_TYPES,
    required: true
  },
  visibility: {
    type: String,
    enum: ['member', 'admin', 'public'],
    default: 'member'
  },
  metadata: {
    type: mongoose.Schema.Types.Mixed,
    default: () => ({})
  },
  performedBy: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Member'
  },
  timestamp: {
    type: Date,
    default: Date.now
  }
})

// Indexes
activityLogSchema.index({ member: 1, timestamp: -1 })
activityLogSchema.index({ member: 1, visibility: 1, timestamp: -1 })
activityLogSchema.index({ type: 1, timestamp: -1 })

export default mongoose.models.ActivityLog || mongoose.model('ActivityLog', activityLogSchema)
