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

currency: {
        type: DataTypes.TEXT,
      
      

      },

available_balance: {
        type: DataTypes.DECIMAL,
      
      

      },

pending_balance: {
        type: DataTypes.DECIMAL,
      
      

      },

lifetime_earned: {
        type: DataTypes.DECIMAL,
      
      

      },

lifetime_paid_out: {
        type: DataTypes.DECIMAL,
      
      

      },

minimum_withdrawal_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  wallets.associate = (db) => {


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



























    db.wallets.hasMany(db.wallet_transactions, {
      as: 'wallet_transactions_wallet',
      foreignKey: {
          name: 'walletId',
      },
      constraints: false,
    });



    db.wallets.hasMany(db.withdrawal_requests, {
      as: 'withdrawal_requests_wallet',
      foreignKey: {
          name: 'walletId',
      },
      constraints: false,
    });
















//end loop



    db.wallets.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.wallets.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return wallets;
};


