import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument, Types } from 'mongoose';

export type AuditLogDocument = HydratedDocument<AuditLog>;

@Schema({ timestamps: true, versionKey: false })
export class AuditLog {
  @Prop({ required: true, index: true })
  actorType!: 'user' | 'superadmin' | 'system';

  @Prop({ type: Types.ObjectId, required: false, index: true })
  actorUserId?: Types.ObjectId;

  @Prop({ type: String, required: false, index: true })
  actorIdentifier?: string;

  @Prop({ required: true, index: true })
  action!: string;

  @Prop({ required: true, index: true })
  targetType!: string;

  @Prop({ type: String, required: false, index: true })
  targetId?: string;

  @Prop({ type: Object, default: {} })
  metadata!: Record<string, unknown>;
}

export const AuditLogSchema = SchemaFactory.createForClass(AuditLog);
AuditLogSchema.index({ createdAt: -1, action: 1 });
