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

title: {
        type: DataTypes.TEXT,
      
      

      },

build_mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"manual",


"automatic"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"ready",


"exported",


"archived"

        ],

      },

target_question_count: {
        type: DataTypes.INTEGER,
      
      

      },

target_total_points: {
        type: DataTypes.DECIMAL,
      
      

      },

target_average_difficulty: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

      },

assigned_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

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

  tests.associate = (db) => {

    db.tests.belongsToMany(db.skills, {
      as: 'skills',
      foreignKey: {
        name: 'tests_skillsId',
      },
      constraints: false,
      through: 'testsSkillsSkills',
    });

    db.tests.belongsToMany(db.skills, {
      as: 'skills_filter',
      foreignKey: {
        name: 'tests_skillsId',
      },
      constraints: false,
      through: 'testsSkillsSkills',
    });


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















    db.tests.hasMany(db.test_sections, {
      as: 'test_sections_test',
      foreignKey: {
          name: 'testId',
      },
      constraints: false,
    });


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


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












//end loop



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

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

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

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

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




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

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



  return tests;
};


