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

version_label: {
        type: DataTypes.TEXT,
      
      

      },

generation_strategy: {
        type: DataTypes.ENUM,
      
      

        values: [

"shuffle_questions",


"shuffle_answers",


"both",


"custom"

        ],

      },

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

      },

generated_at: {
        type: DataTypes.DATE,
      
      

      },

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

  test_versions.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_versions.hasMany(db.version_items, {
      as: 'version_items_test_version',
      foreignKey: {
          name: 'test_versionId',
      },
      constraints: false,
    });


    db.test_versions.hasMany(db.version_answer_orders, {
      as: 'version_answer_orders_test_version',
      foreignKey: {
          name: 'test_versionId',
      },
      constraints: false,
    });


    db.test_versions.hasMany(db.exports, {
      as: 'exports_test_version',
      foreignKey: {
          name: 'test_versionId',
      },
      constraints: false,
    });


    db.test_versions.hasMany(db.answer_keys, {
      as: 'answer_keys_test_version',
      foreignKey: {
          name: 'test_versionId',
      },
      constraints: false,
    });


    db.test_versions.hasMany(db.scoring_schemes, {
      as: 'scoring_schemes_test_version',
      foreignKey: {
          name: 'test_versionId',
      },
      constraints: false,
    });


    db.test_versions.hasMany(db.test_matrices, {
      as: 'test_matrices_test_version',
      foreignKey: {
          name: 'test_versionId',
      },
      constraints: false,
    });






//end loop



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



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

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


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

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



  return test_versions;
};


