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

title: {
        type: DataTypes.TEXT,
      
      

      },

mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"chat",


"voice"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"archived",


"deleted"

        ],

      },

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

      },

last_activity_at: {
        type: DataTypes.DATE,
      
      

      },

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

  conversations.associate = (db) => {


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










    db.conversations.hasMany(db.messages, {
      as: 'messages_conversation',
      foreignKey: {
          name: 'conversationId',
      },
      constraints: false,
    });


    db.conversations.hasMany(db.voice_sessions, {
      as: 'voice_sessions_conversation',
      foreignKey: {
          name: 'conversationId',
      },
      constraints: false,
    });







    db.conversations.hasMany(db.tasks, {
      as: 'tasks_conversation',
      foreignKey: {
          name: 'conversationId',
      },
      constraints: false,
    });





    db.conversations.hasMany(db.meetings, {
      as: 'meetings_conversation',
      foreignKey: {
          name: 'conversationId',
      },
      constraints: false,
    });











//end loop



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

    db.conversations.belongsTo(db.assistant_profiles, {
      as: 'assistant_profile',
      foreignKey: {
        name: 'assistant_profileId',
      },
      constraints: false,
    });

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




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

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



  return conversations;
};


