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: {
        type: DataTypes.TEXT,
      
      

      },

last_message_at: {
        type: DataTypes.DATE,
      
      

      },

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

  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.conversations.hasMany(db.conversation_members, {
      as: 'conversation_members_conversation',
      foreignKey: {
          name: 'conversationId',
      },
      constraints: false,
    });


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


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







//end loop



    db.conversations.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });



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


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

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



  return conversations;
};


