import mongoose from "mongoose";

const updateSchema = new mongoose.Schema({
  author: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Member",
    required: true,
  },
  content: {
    type: String,
    required: true,
  },
  images: [
    {
      url: String,
      publicId: String,
      alt: String,
    },
  ],
  privacy: {
    type: String,
    enum: ["public", "members", "private"],
    default: "members",
  },
  commentsEnabled: {
    type: Boolean,
    default: true,
  },
  createdAt: {
    type: Date,
    default: Date.now,
  },
  updatedAt: {
    type: Date,
    default: Date.now,
  },
});

// Update the updatedAt timestamp on save
updateSchema.pre("save", function (next) {
  this.updatedAt = Date.now();
  next();
});

// Indexes for performance
updateSchema.index({ createdAt: -1 }); // For sorting by date
updateSchema.index({ privacy: 1, createdAt: -1 }); // Compound index for filtering and sorting
updateSchema.index({ author: 1 }); // For author lookups

export default mongoose.models.Update || mongoose.model("Update", updateSchema);
