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

conversation_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"direct",


"group"

        ],

      },

title_text: {
        type: DataTypes.TEXT,
      
      

      },

last_message_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

      },

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

  conversations.associate = (db) => {

    db.conversations.belongsToMany(db.users, {
      as: 'participants',
      foreignKey: {
        name: 'conversations_participantsId',
      },
      constraints: false,
      through: 'conversationsParticipantsUsers',
    });

    db.conversations.belongsToMany(db.users, {
      as: 'participants_filter',
      foreignKey: {
        name: 'conversations_participantsId',
      },
      constraints: false,
      through: 'conversationsParticipantsUsers',
    });


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










    db.conversations.hasMany(db.messages, {
      as: 'messages_conversation',
      foreignKey: {
          name: 'conversationId',
      },
      constraints: false,
    });





    db.conversations.hasMany(db.notifications, {
      as: 'notifications_conversation',
      foreignKey: {
          name: 'conversationId',
      },
      constraints: false,
    });














//end loop



    db.conversations.belongsTo(db.groups, {
      as: 'group',
      foreignKey: {
        name: 'groupId',
      },
      constraints: false,
    });




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

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



  return conversations;
};


