import mongoose, { Schema, models, model } from "mongoose";

export interface TeamActivityDoc {
  _id: string;
  actorId: mongoose.Types.ObjectId;
  actorName: string;
  action: string;
  targetLabel: string;
  createdAt: Date;
}

const teamActivitySchema = new Schema<TeamActivityDoc>(
  {
    actorId: { type: Schema.Types.ObjectId, ref: "Client", required: true },
    actorName: { type: String, required: true, trim: true },
    action: { type: String, required: true, trim: true, maxlength: 200 },
    targetLabel: { type: String, default: "", trim: true, maxlength: 200 },
  },
  { timestamps: { createdAt: true, updatedAt: false } }
);

// Das Systemprotokoll (lib/logFeed.ts) filtert/sortiert nach createdAt.
teamActivitySchema.index({ createdAt: -1 });

export const TeamActivity =
  (models.TeamActivity as mongoose.Model<TeamActivityDoc>) || model<TeamActivityDoc>("TeamActivity", teamActivitySchema);
