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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"answering",


"wins",


"boxes",


"yux",


"modes",


"community",


"creation",


"streaks"

        ],

      },

target_value: {
        type: DataTypes.INTEGER,
      
      

      },

reward_yux: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  achievements.associate = (db) => {


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















    db.achievements.hasMany(db.user_achievements, {
      as: 'user_achievements_achievement',
      foreignKey: {
          name: 'achievementId',
      },
      constraints: false,
    });























//end loop



    db.achievements.belongsTo(db.titles, {
      as: 'reward_title',
      foreignKey: {
        name: 'reward_titleId',
      },
      constraints: false,
    });

    db.achievements.belongsTo(db.badges, {
      as: 'reward_badge',
      foreignKey: {
        name: 'reward_badgeId',
      },
      constraints: false,
    });




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

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



  return achievements;
};


