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 chat_rooms = sequelize.define(
    'chat_rooms',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

room_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"PUBLIC",


"DIRECT",


"GROUP"

        ],

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

private_room: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

created_at_time: {
        type: DataTypes.DATE,
      
      

      },

last_message_at: {
        type: DataTypes.DATE,
      
      

      },

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

  chat_rooms.associate = (db) => {


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
























    db.chat_rooms.hasMany(db.chat_memberships, {
      as: 'chat_memberships_chat_room',
      foreignKey: {
          name: 'chat_roomId',
      },
      constraints: false,
    });


    db.chat_rooms.hasMany(db.chat_messages, {
      as: 'chat_messages_chat_room',
      foreignKey: {
          name: 'chat_roomId',
      },
      constraints: false,
    });





//end loop



    db.chat_rooms.belongsTo(db.users, {
      as: 'created_by_user',
      foreignKey: {
        name: 'created_by_userId',
      },
      constraints: false,
    });




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

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



  return chat_rooms;
};


