// chat-message.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0

'use strict';

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

/*
 * The intent is for forked apps to give meaning to "channel" in their
 * apps. Set the channelType to the name of your channel model, and set
 * channel to the _id of the channel. The model will then correctly populate.
 */

const ChatMessageSchema = new Schema({
  created: { type: Date, default: Date.now, required: true, index: -1 },
  channelType: { type: String },
  channel: { type: Schema.ObjectId, refPath: 'channelType' },
  authorType: { type: String, enum: ['User', 'CoreUser'], required: true },
  author: { type: Schema.ObjectId, required: true, index: 1, refPath: 'authorType' },
  content: { type: String, maxlength: 1000 },
  analysis: {
    similarity: { type: Number },
  },
  stickers: { type: [String] },
  attachments: { type: [Schema.ObjectId], ref: 'Attachment' },
});

module.exports = mongoose.model('ChatMessage', ChatMessageSchema);