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

session_key: {
        type: DataTypes.TEXT,
      
      

      },

channel: {
        type: DataTypes.ENUM,
      
      

        values: [

"website_widget",


"order_status_page",


"email_link"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"closed",


"handoff"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

ended_at: {
        type: DataTypes.DATE,
      
      

      },

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

  chat_sessions.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_sessions.hasMany(db.chat_messages, {
      as: 'chat_messages_session',
      foreignKey: {
          name: 'sessionId',
      },
      constraints: false,
    });





//end loop



    db.chat_sessions.belongsTo(db.stores, {
      as: 'store',
      foreignKey: {
        name: 'storeId',
      },
      constraints: false,
    });

    db.chat_sessions.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });

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




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

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



  return chat_sessions;
};


