const mongoose = require('mongoose')
const { Schema } = mongoose

const messageSchema = new Schema({
  senderId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  receiverId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  content: {
    type: String,
    required: false,
    trim: true
  },
  file: {
    type: String,
    required: false
  },
  fileType: {
    type: String,
    enum: ['image', 'video', 'file', 'voice', 'location'],
    required: false
  },
  replyToId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Message',
    required: false
  },
  forwardedFrom: {
    userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    userName: String,
    displayName: String
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  readStatus: {
    type: Number,
    default: 0
  }
})

const MessageModel = mongoose.model('Message', messageSchema)

module.exports = MessageModel
