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

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

points: {
        type: DataTypes.DECIMAL,
      
      

      },

override_content: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

scoring_rubric: {
        type: DataTypes.TEXT,
      
      

      },

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

  test_items.associate = (db) => {


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


















    db.test_items.hasMany(db.version_items, {
      as: 'version_items_test_item',
      foreignKey: {
          name: 'test_itemId',
      },
      constraints: false,
    });







    db.test_items.hasMany(db.matrix_rows, {
      as: 'matrix_rows_test_item',
      foreignKey: {
          name: 'test_itemId',
      },
      constraints: false,
    });





//end loop



    db.test_items.belongsTo(db.tests, {
      as: 'test',
      foreignKey: {
        name: 'testId',
      },
      constraints: false,
    });

    db.test_items.belongsTo(db.test_sections, {
      as: 'section',
      foreignKey: {
        name: 'sectionId',
      },
      constraints: false,
    });

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




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

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



  return test_items;
};


