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

session_title: {
        type: DataTypes.TEXT,
      
      

      },

channel: {
        type: DataTypes.ENUM,
      
      

        values: [

"web",


"mobile",


"api"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

last_message_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_session',
      foreignKey: {
          name: 'sessionId',
      },
      constraints: false,
    });









//end loop



    db.ai_chat_sessions.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.ai_chat_sessions.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

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




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

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



  return ai_chat_sessions;
};


