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

started_at: {
        type: DataTypes.DATE,
      
      

      },

ended_at: {
        type: DataTypes.DATE,
      
      

      },

completion_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"in_progress",


"completed",


"abandoned"

        ],

      },

total_score: {
        type: DataTypes.DECIMAL,
      
      

      },

session_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  cognitive_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.cognitive_sessions.hasMany(db.cognitive_attempts, {
      as: 'cognitive_attempts_session',
      foreignKey: {
          name: 'sessionId',
      },
      constraints: false,
    });















//end loop



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

    db.cognitive_sessions.belongsTo(db.cognitive_programs, {
      as: 'program',
      foreignKey: {
        name: 'programId',
      },
      constraints: false,
    });

    db.cognitive_sessions.belongsTo(db.users, {
      as: 'started_by_user',
      foreignKey: {
        name: 'started_by_userId',
      },
      constraints: false,
    });

    db.cognitive_sessions.belongsTo(db.accounts, {
      as: 'accounts',
      foreignKey: {
        name: 'accountsId',
      },
      constraints: false,
    });




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

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



  return cognitive_sessions;
};


