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

thread_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"direct",


"group",


"support"

        ],

      },

thread_title: {
        type: DataTypes.TEXT,
      
      

      },

last_message_at: {
        type: DataTypes.DATE,
      
      

      },

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_threads.associate = (db) => {

    db.chat_threads.belongsToMany(db.users, {
      as: 'participants',
      foreignKey: {
        name: 'chat_threads_participantsId',
      },
      constraints: false,
      through: 'chat_threadsParticipantsUsers',
    });

    db.chat_threads.belongsToMany(db.users, {
      as: 'participants_filter',
      foreignKey: {
        name: 'chat_threads_participantsId',
      },
      constraints: false,
      through: 'chat_threadsParticipantsUsers',
    });


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

















    db.chat_threads.hasMany(db.messages, {
      as: 'messages_chat_thread',
      foreignKey: {
          name: 'chat_threadId',
      },
      constraints: false,
    });











//end loop






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

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



  return chat_threads;
};


