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,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

quiz_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"official",


"community",


"personal"

        ],

      },

visibility: {
        type: DataTypes.ENUM,
      
      

        values: [

"private",


"unlisted",


"public"

        ],

      },

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

      },

verified_badge_text: {
        type: DataTypes.TEXT,
      
      

      },

subject: {
        type: DataTypes.ENUM,
      
      

        values: [

"mathematics",


"science",


"history",


"geography",


"technology",


"animals",


"space",


"gaming",


"movies",


"music",


"languages",


"other"

        ],

      },

age_group: {
        type: DataTypes.ENUM,
      
      

        values: [

"early",


"kids",


"teens",


"adult",


"mixed"

        ],

      },

difficulty: {
        type: DataTypes.ENUM,
      
      

        values: [

"easy",


"medium",


"hard",


"mixed"

        ],

      },

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

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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


    db.quizzes.hasMany(db.orbix_quiz_assistant_sessions, {
      as: 'orbix_quiz_assistant_sessions_generated_quiz',
      foreignKey: {
          name: 'generated_quizId',
      },
      constraints: false,
    });



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















//end loop



    db.quizzes.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });



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


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

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



  return quizzes;
};


