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: [

"quiz",


"test",


"open_questions",


"exercises"

        ],

      },

difficulty_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"intro",


"intermediate",


"advanced"

        ],

      },

time_limit_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

      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_questions, {
      as: 'quiz_questions_quiz',
      foreignKey: {
          name: 'quizId',
      },
      constraints: false,
    });


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








//end loop



    db.quizzes.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

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

    db.quizzes.belongsTo(db.topics, {
      as: 'topic',
      foreignKey: {
        name: 'topicId',
      },
      constraints: false,
    });

    db.quizzes.belongsTo(db.sources, {
      as: 'source',
      foreignKey: {
        name: 'sourceId',
      },
      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;
};


