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

title: {
        type: DataTypes.TEXT,
      
      

      },

mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"paper",


"cbt"

        ],

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

duration_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

passing_score: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

  exams.associate = (db) => {


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





















    db.exams.hasMany(db.exam_questions, {
      as: 'exam_questions_exam',
      foreignKey: {
          name: 'examId',
      },
      constraints: false,
    });



    db.exams.hasMany(db.exam_attempts, {
      as: 'exam_attempts_exam',
      foreignKey: {
          name: 'examId',
      },
      constraints: false,
    });








//end loop



    db.exams.belongsTo(db.class_subjects, {
      as: 'class_subject',
      foreignKey: {
        name: 'class_subjectId',
      },
      constraints: false,
    });

    db.exams.belongsTo(db.exam_categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      constraints: false,
    });

    db.exams.belongsTo(db.users, {
      as: 'created_by',
      foreignKey: {
        name: 'created_byId',
      },
      constraints: false,
    });

    db.exams.belongsTo(db.schools, {
      as: 'schools',
      foreignKey: {
        name: 'schoolsId',
      },
      constraints: false,
    });




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

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



  return exams;
};


