// src/models/socket-message.ts
// Copyright (C) 2025 DTP Technologies, LLC
// All Rights Reserved

import { Schema, model, Types, Document } from "mongoose";

export interface ISocketMessage extends Document {
  _id: Types.ObjectId;
  userId: Types.ObjectId;
  event: string;
  data: any;
  createdAt: Date;
}

export const SocketMessageSchema = new Schema<ISocketMessage>(
  {
    userId: {
      type: Schema.Types.ObjectId,
      ref: "User",
      required: true,
      index: true,
    },
    event: {
      type: String,
      required: true,
    },
    data: {
      type: Schema.Types.Mixed,
      required: true,
    },
  },
  {
    timestamps: { createdAt: true, updatedAt: false },
  },
);

// TTL index to auto-expire messages after 24 hours
SocketMessageSchema.index({ createdAt: 1 }, { expireAfterSeconds: 86400 });

// Compound index for efficient querying by user and creation time
SocketMessageSchema.index({ userId: 1, createdAt: 1 });

export const SocketMessage = model<ISocketMessage>(
  "SocketMessage",
  SocketMessageSchema,
);
export default SocketMessage;
