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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

unlock_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"achievement",


"event",


"shop",


"starter",


"admin_grant"

        ],

      },

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

  badges.associate = (db) => {


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











    db.badges.hasMany(db.inventory_items, {
      as: 'inventory_items_badge',
      foreignKey: {
          name: 'badgeId',
      },
      constraints: false,
    });




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


















    db.badges.hasMany(db.daily_rewards, {
      as: 'daily_rewards_badge',
      foreignKey: {
          name: 'badgeId',
      },
      constraints: false,
    });







//end loop





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


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

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



  return badges;
};


