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

name: {
        type: DataTypes.TEXT,
      
      

      },

channel_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"general",


"team",


"breakout",


"direct",


"system"

        ],

      },

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

      },

last_message_at: {
        type: DataTypes.DATE,
      
      

      },

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

  chat_channels.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_channels.hasMany(db.chat_messages, {
      as: 'chat_messages_chat_channel',
      foreignKey: {
          name: 'chat_channelId',
      },
      constraints: false,
    });

































//end loop



    db.chat_channels.belongsTo(db.conferences, {
      as: 'conference',
      foreignKey: {
        name: 'conferenceId',
      },
      constraints: false,
    });

    db.chat_channels.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return chat_channels;
};


