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

state: {
        type: DataTypes.ENUM,
      
      

        values: [

"idle",


"listening",


"processing",


"speaking",


"ended",


"error"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

ended_at: {
        type: DataTypes.DATE,
      
      

      },

stt_provider: {
        type: DataTypes.TEXT,
      
      

      },

tts_provider: {
        type: DataTypes.TEXT,
      
      

      },

detected_language: {
        type: DataTypes.TEXT,
      
      

      },

last_error: {
        type: DataTypes.TEXT,
      
      

      },

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

  voice_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.voice_sessions.hasMany(db.messages, {
      as: 'messages_voice_session',
      foreignKey: {
          name: 'voice_sessionId',
      },
      constraints: false,
    });






















//end loop



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

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



    db.voice_sessions.hasMany(db.file, {
      as: 'audio_recordings',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.voice_sessions.getTableName(),
        belongsToColumn: 'audio_recordings',
      },
    });


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

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



  return voice_sessions;
};


