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

name: {
        type: DataTypes.TEXT,
      
      

      },

rarity: {
        type: DataTypes.ENUM,
      
      

        values: [

"common",


"uncommon",


"rare",


"epic",


"legendary",


"mythic"

        ],

      },

source_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"starter",


"box",


"event",


"achievement",


"shop"

        ],

      },

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

      },

model_key: {
        type: DataTypes.TEXT,
      
      

      },

drop_weight: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  avatars.associate = (db) => {


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









    db.avatars.hasMany(db.box_items, {
      as: 'box_items_avatar',
      foreignKey: {
          name: 'avatarId',
      },
      constraints: false,
    });



    db.avatars.hasMany(db.inventory_items, {
      as: 'inventory_items_avatar',
      foreignKey: {
          name: 'avatarId',
      },
      constraints: false,
    });



























//end loop





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


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

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



  return avatars;
};


