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

title: {
        type: DataTypes.TEXT,
      
      

      },

quiz_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"poll",


"quiz",


"survey"

        ],

      },

quiz_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"closed",


"archived"

        ],

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

h5p_reference: {
        type: DataTypes.TEXT,
      
      

      },

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

  quizzes.associate = (db) => {


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










































    db.quizzes.hasMany(db.quiz_results, {
      as: 'quiz_results_quiz',
      foreignKey: {
          name: 'quizId',
      },
      constraints: false,
    });














//end loop



    db.quizzes.belongsTo(db.courses, {
      as: 'course',
      foreignKey: {
        name: 'courseId',
      },
      constraints: false,
    });

    db.quizzes.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return quizzes;
};


