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

question_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"pilihan_ganda",


"essay"

        ],

      },

question_text: {
        type: DataTypes.TEXT,
      
      

      },

points: {
        type: DataTypes.DECIMAL,
      
      

      },

order_number: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  exam_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.exam_questions.hasMany(db.exam_question_choices, {
      as: 'exam_question_choices_exam_question',
      foreignKey: {
          name: 'exam_questionId',
      },
      constraints: false,
    });



    db.exam_questions.hasMany(db.exam_answers, {
      as: 'exam_answers_exam_question',
      foreignKey: {
          name: 'exam_questionId',
      },
      constraints: false,
    });







//end loop



    db.exam_questions.belongsTo(db.exams, {
      as: 'exam',
      foreignKey: {
        name: 'examId',
      },
      constraints: false,
    });

    db.exam_questions.belongsTo(db.schools, {
      as: 'schools',
      foreignKey: {
        name: 'schoolsId',
      },
      constraints: false,
    });



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


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

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



  return exam_questions;
};


