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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

box_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"racecrew",


"kingdom",


"wheel_of_fortune",


"future",


"event"

        ],

      },

price_yux: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  boxes.associate = (db) => {


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









    db.boxes.hasMany(db.box_items, {
      as: 'box_items_box',
      foreignKey: {
          name: 'boxId',
      },
      constraints: false,
    });


    db.boxes.hasMany(db.purchases, {
      as: 'purchases_box',
      foreignKey: {
          name: 'boxId',
      },
      constraints: false,
    });






















    db.boxes.hasMany(db.daily_rewards, {
      as: 'daily_rewards_box',
      foreignKey: {
          name: 'boxId',
      },
      constraints: false,
    });







//end loop





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


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

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



  return boxes;
};


