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

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"direct",


"group"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  chat_conversations.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_conversations.hasMany(db.chat_participants, {
      as: 'chat_participants_conversation',
      foreignKey: {
          name: 'conversationId',
      },
      constraints: false,
    });


    db.chat_conversations.hasMany(db.chat_messages, {
      as: 'chat_messages_conversation',
      foreignKey: {
          name: 'conversationId',
      },
      constraints: false,
    });





//end loop



    db.chat_conversations.belongsTo(db.businesses, {
      as: 'business',
      foreignKey: {
        name: 'businessId',
      },
      constraints: false,
    });




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

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



  return chat_conversations;
};


