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

title: {
        type: DataTypes.TEXT,
      
      

      },

difficulty: {
        type: DataTypes.ENUM,
      
      

        values: [

"easy",


"medium",


"hard"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"archived"

        ],

      },

question_count: {
        type: DataTypes.INTEGER,
      
      

      },

time_limit_seconds: {
        type: DataTypes.INTEGER,
      
      

      },

generated_at: {
        type: DataTypes.DATE,
      
      

      },

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

  quiz_definitions.associate = (db) => {


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

























    db.quiz_definitions.hasMany(db.quiz_questions, {
      as: 'quiz_questions_quiz_definition',
      foreignKey: {
          name: 'quiz_definitionId',
      },
      constraints: false,
    });



    db.quiz_definitions.hasMany(db.quiz_attempts, {
      as: 'quiz_attempts_quiz_definition',
      foreignKey: {
          name: 'quiz_definitionId',
      },
      constraints: false,
    });













//end loop



    db.quiz_definitions.belongsTo(db.books, {
      as: 'book',
      foreignKey: {
        name: 'bookId',
      },
      constraints: false,
    });




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

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



  return quiz_definitions;
};


