import mongoConnection from "$connections/mongo/mongo.connection.js";

import mongoose from "mongoose";
const mongooseSchema = mongoose.Schema;

export const schemaModel = {
  message: {
    type: mongooseSchema.Types.ObjectId,
    ref: "Message",
    required: true,
    index: true,
  },
  user: {
    type: mongooseSchema.Types.ObjectId,
    ref: "User",
    required: true,
    index: true,
  },
  readAt: {
    type: Date,
    default: Date.now,
    required: true,
  },
};

export const schema = new mongooseSchema(schemaModel, { timestamps: true });

schema.index({ message: 1, user: 1 }, { unique: true });

export default mongoConnection.model("MessageRead", schema);
