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

answer_text: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

order_index: {
        type: DataTypes.INTEGER,
      
      

      },

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

  question_answers.associate = (db) => {


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


























    db.question_answers.hasMany(db.player_answers, {
      as: 'player_answers_selected_answer',
      foreignKey: {
          name: 'selected_answerId',
      },
      constraints: false,
    });












//end loop



    db.question_answers.belongsTo(db.quiz_questions, {
      as: 'question',
      foreignKey: {
        name: 'questionId',
      },
      constraints: false,
    });



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


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

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



  return question_answers;
};


