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

quiz_title: {
        type: DataTypes.TEXT,
      
      

      },

grading_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"auto",


"manual"

        ],

      },

passing_score: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

  quizzes.associate = (db) => {


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


















    db.quizzes.hasMany(db.quiz_questions, {
      as: 'quiz_questions_quiz',
      foreignKey: {
          name: 'quizId',
      },
      constraints: false,
    });



    db.quizzes.hasMany(db.quiz_submissions, {
      as: 'quiz_submissions_quiz',
      foreignKey: {
          name: 'quizId',
      },
      constraints: false,
    });




























//end loop



    db.quizzes.belongsTo(db.missions, {
      as: 'mission',
      foreignKey: {
        name: 'missionId',
      },
      constraints: false,
    });

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




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

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



  return quizzes;
};


