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

attempt_kind: {
        type: DataTypes.ENUM,
      
      

        values: [

"exercise",


"game",


"quiz",


"activity"

        ],

      },

score: {
        type: DataTypes.INTEGER,
      
      

      },

accuracy_percent: {
        type: DataTypes.DECIMAL,
      
      

      },

duration_seconds: {
        type: DataTypes.INTEGER,
      
      

      },

result: {
        type: DataTypes.ENUM,
      
      

        values: [

"success",


"fail",


"timeout",


"abandoned"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

finished_at: {
        type: DataTypes.DATE,
      
      

      },

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

  attempts.associate = (db) => {


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
































//end loop



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

    db.attempts.belongsTo(db.exercises, {
      as: 'exercise',
      foreignKey: {
        name: 'exerciseId',
      },
      constraints: false,
    });

    db.attempts.belongsTo(db.games, {
      as: 'game',
      foreignKey: {
        name: 'gameId',
      },
      constraints: false,
    });

    db.attempts.belongsTo(db.interactive_activities, {
      as: 'activity',
      foreignKey: {
        name: 'activityId',
      },
      constraints: false,
    });

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




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

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



  return attempts;
};


