import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument, Schema as MongooseSchema } from 'mongoose';

export type AuditLogDocument = HydratedDocument<AuditLog>;

@Schema({ timestamps: { createdAt: true, updatedAt: false }, collection: 'audit_logs' })
export class AuditLog {
  @Prop({ index: true })
  actorUserId?: string;

  @Prop()
  actorEmail?: string;

  @Prop({ required: true, index: true })
  action: string;

  @Prop()
  targetType?: string;

  @Prop()
  targetId?: string;

  @Prop({ type: MongooseSchema.Types.Mixed })
  metadata?: Record<string, unknown>;

  @Prop()
  ipAddress?: string;

  @Prop()
  userAgent?: string;
}

export const AuditLogSchema = SchemaFactory.createForClass(AuditLog);
AuditLogSchema.index({ actorUserId: 1, createdAt: -1 });
AuditLogSchema.index({ createdAt: -1 });
