const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const mentor_messages = sequelize.define(
    'mentor_messages',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

sender_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"user",


"mentor",


"system"

        ],

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

sent_at: {
        type: DataTypes.DATE,
      
      

      },

tokens_used: {
        type: DataTypes.INTEGER,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  mentor_messages.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
































//end loop



    db.mentor_messages.belongsTo(db.mentor_conversations, {
      as: 'conversation',
      foreignKey: {
        name: 'conversationId',
      },
      constraints: false,
    });

    db.mentor_messages.belongsTo(db.users, {
      as: 'sender_user',
      foreignKey: {
        name: 'sender_userId',
      },
      constraints: false,
    });

    db.mentor_messages.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



    db.mentor_messages.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.mentor_messages.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


    db.mentor_messages.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.mentor_messages.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };



  return mentor_messages;
};


