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_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"doctor_patient",


"doctor_doctor",


"patient_chatbot"

        ],

      },

session_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"closed",


"archived"

        ],

      },

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.messages, {
      as: 'messages_chat_session',
      foreignKey: {
          name: 'chat_sessionId',
      },
      constraints: false,
    });


    db.chat_sessions.hasMany(db.video_calls, {
      as: 'video_calls_chat_session',
      foreignKey: {
          name: 'chat_sessionId',
      },
      constraints: false,
    });


    db.chat_sessions.hasMany(db.chatbot_interactions, {
      as: 'chatbot_interactions_chat_session',
      foreignKey: {
          name: 'chat_sessionId',
      },
      constraints: false,
    });




//end loop



    db.chat_sessions.belongsTo(db.patient_profiles, {
      as: 'patient',
      foreignKey: {
        name: 'patientId',
      },
      constraints: false,
    });

    db.chat_sessions.belongsTo(db.doctor_profiles, {
      as: 'doctor',
      foreignKey: {
        name: 'doctorId',
      },
      constraints: false,
    });

    db.chat_sessions.belongsTo(db.doctor_profiles, {
      as: 'second_doctor',
      foreignKey: {
        name: 'second_doctorId',
      },
      constraints: false,
    });

    db.chat_sessions.belongsTo(db.users, {
      as: 'created_by_user',
      foreignKey: {
        name: 'created_by_userId',
      },
      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;
};


