const mongoose = require('mongoose');

const activityLogSchema = new mongoose.Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  companyId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Company'
  },
  action: {
    type: String,
    required: true,
    enum: ['login', 'logout', 'create', 'update', 'delete', 'approve', 'reject', 'review', 'download', 'share']
  },
  module: {
    type: String,
    required: true
  },
  entityType: String,
  entityId: mongoose.Schema.Types.ObjectId,
  description: String,
  ipAddress: String,
  userAgent: String,
  changes: mongoose.Schema.Types.Mixed
}, {
  timestamps: true
});

activityLogSchema.index({ userId: 1, createdAt: -1 });
activityLogSchema.index({ companyId: 1, createdAt: -1 });
activityLogSchema.index({ action: 1, createdAt: -1 });

module.exports = mongoose.model('ActivityLog', activityLogSchema);