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

channel: {
        type: DataTypes.ENUM,
      
      

        values: [

"web_chat",


"whatsapp",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"closed",


"archived"

        ],

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

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

  ai_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.ai_chat_sessions.hasMany(db.ai_chat_messages, {
      as: 'ai_chat_messages_ai_chat_session',
      foreignKey: {
          name: 'ai_chat_sessionId',
      },
      constraints: false,
    });




//end loop



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




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

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



  return ai_chat_sessions;
};


