import { Schema } from "mongoose";
import { applyIdTransform, registerModel, stringId } from "../schema";

const ActivityLogSchema = new Schema(
  {
    _id: stringId,
    actorId: { type: String, ref: "User", required: true, index: true },
    actorEmail: { type: String, required: true },
    actorName: { type: String, default: "" },
    action: { type: String, required: true, index: true },
    entityType: { type: String, required: true, index: true },
    entityId: { type: String, default: undefined, index: true },
    meta: { type: Schema.Types.Mixed, default: undefined },
  },
  { timestamps: { createdAt: true, updatedAt: false } },
);

ActivityLogSchema.index({ createdAt: -1 });
applyIdTransform(ActivityLogSchema);

export const ActivityLog = registerModel("ActivityLog", ActivityLogSchema);
