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

name: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

  question_banks.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_banks.hasMany(db.questions, {
      as: 'questions_question_bank',
      foreignKey: {
          name: 'question_bankId',
      },
      constraints: false,
    });

















//end loop



    db.question_banks.belongsTo(db.subjects, {
      as: 'subject',
      foreignKey: {
        name: 'subjectId',
      },
      constraints: false,
    });

    db.question_banks.belongsTo(db.grade_levels, {
      as: 'grade_level',
      foreignKey: {
        name: 'grade_levelId',
      },
      constraints: false,
    });

    db.question_banks.belongsTo(db.textbooks, {
      as: 'textbook',
      foreignKey: {
        name: 'textbookId',
      },
      constraints: false,
    });

    db.question_banks.belongsTo(db.users, {
      as: 'owner_user',
      foreignKey: {
        name: 'owner_userId',
      },
      constraints: false,
    });




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

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



  return question_banks;
};


