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,
      },

last_message: {
        type: DataTypes.TEXT,
      
      

      },

last_message_at: {
        type: DataTypes.DATE,
      
      

      },

tutor_unread_count: {
        type: DataTypes.INTEGER,
      
      

      },

learner_unread_count: {
        type: DataTypes.INTEGER,
      
      

      },

      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.messages, {
      as: 'messages_conversation',
      foreignKey: {
          name: 'conversationId',
      },
      constraints: false,
    });






























//end loop



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

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




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

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



  return conversations;
};


