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

question_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"single_choice",


"multiple_choice",


"true_false",


"short_answer",


"open_ended",


"matching",


"ordering"

        ],

      },

difficulty: {
        type: DataTypes.ENUM,
      
      

        values: [

"very_easy",


"easy",


"medium",


"hard",


"very_hard"

        ],

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

default_points: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

hint: {
        type: DataTypes.TEXT,
      
      

      },

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

  questions.associate = (db) => {

    db.questions.belongsToMany(db.skills, {
      as: 'skills',
      foreignKey: {
        name: 'questions_skillsId',
      },
      constraints: false,
      through: 'questionsSkillsSkills',
    });

    db.questions.belongsToMany(db.skills, {
      as: 'skills_filter',
      foreignKey: {
        name: 'questions_skillsId',
      },
      constraints: false,
      through: 'questionsSkillsSkills',
    });


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













    db.questions.hasMany(db.answer_choices, {
      as: 'answer_choices_question',
      foreignKey: {
          name: 'questionId',
      },
      constraints: false,
    });




    db.questions.hasMany(db.test_items, {
      as: 'test_items_question',
      foreignKey: {
          name: 'questionId',
      },
      constraints: false,
    });




    db.questions.hasMany(db.version_answer_orders, {
      as: 'version_answer_orders_question',
      foreignKey: {
          name: 'questionId',
      },
      constraints: false,
    });










//end loop



    db.questions.belongsTo(db.question_banks, {
      as: 'question_bank',
      foreignKey: {
        name: 'question_bankId',
      },
      constraints: false,
    });

    db.questions.belongsTo(db.chapters, {
      as: 'chapter',
      foreignKey: {
        name: 'chapterId',
      },
      constraints: false,
    });



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

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


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

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



  return questions;
};


