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

title: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"memory",


"attention",


"logic",


"math",


"language",


"problem_solving"

        ],

      },

difficulty: {
        type: DataTypes.ENUM,
      
      

        values: [

"easy",


"medium",


"hard",


"expert"

        ],

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

prompt_template: {
        type: DataTypes.TEXT,
      
      

      },

time_limit_seconds: {
        type: DataTypes.INTEGER,
      
      

      },

max_score: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"archived"

        ],

      },

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

  exercises.associate = (db) => {


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











    db.exercises.hasMany(db.content_items, {
      as: 'content_items_exercise',
      foreignKey: {
          name: 'exerciseId',
      },
      constraints: false,
    });





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


















//end loop



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




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

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



  return exercises;
};


