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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"reading",


"streak",


"coins",


"social",


"quiz",


"challenge"

        ],

      },

rarity: {
        type: DataTypes.ENUM,
      
      

        values: [

"common",


"rare",


"epic",


"legendary"

        ],

      },

target_value: {
        type: DataTypes.INTEGER,
      
      

      },

metric: {
        type: DataTypes.ENUM,
      
      

        values: [

"books_completed",


"minutes_read",


"coins_earned",


"streak_days",


"quizzes_passed",


"challenges_completed",


"friends_added"

        ],

      },

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

      },

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

  achievement_definitions.associate = (db) => {


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



















    db.achievement_definitions.hasMany(db.user_achievements, {
      as: 'user_achievements_achievement_definition',
      foreignKey: {
          name: 'achievement_definitionId',
      },
      constraints: false,
    });





















//end loop





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


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

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



  return achievement_definitions;
};


