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

prompt: {
        type: DataTypes.TEXT,
      
      

      },

time_limit_seconds: {
        type: DataTypes.INTEGER,
      
      

      },

points: {
        type: DataTypes.INTEGER,
      
      

      },

question_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"multiple_choice"

        ],

      },

order_index: {
        type: DataTypes.INTEGER,
      
      

      },

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

  quiz_questions.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_questions.hasMany(db.question_answers, {
      as: 'question_answers_question',
      foreignKey: {
          name: 'questionId',
      },
      constraints: false,
    });








    db.quiz_questions.hasMany(db.match_questions, {
      as: 'match_questions_quiz_question',
      foreignKey: {
          name: 'quiz_questionId',
      },
      constraints: false,
    });













//end loop



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



    db.quiz_questions.hasMany(db.file, {
      as: 'prompt_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.quiz_questions.getTableName(),
        belongsToColumn: 'prompt_images',
      },
    });


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

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



  return quiz_questions;
};


