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

name: {
        type: DataTypes.TEXT,
      
      

      },

base_currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"USD",


"EUR",


"GBP",


"JPY"

        ],

      },

starting_cash: {
        type: DataTypes.DECIMAL,
      
      

      },

cash_balance: {
        type: DataTypes.DECIMAL,
      
      

      },

buying_power: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

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

  paper_accounts.associate = (db) => {


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


















    db.paper_accounts.hasMany(db.paper_orders, {
      as: 'paper_orders_paper_account',
      foreignKey: {
          name: 'paper_accountId',
      },
      constraints: false,
    });



    db.paper_accounts.hasMany(db.paper_positions, {
      as: 'paper_positions_paper_account',
      foreignKey: {
          name: 'paper_accountId',
      },
      constraints: false,
    });


    db.paper_accounts.hasMany(db.portfolio_snapshots, {
      as: 'portfolio_snapshots_paper_account',
      foreignKey: {
          name: 'paper_accountId',
      },
      constraints: false,
    });

















//end loop



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




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

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



  return paper_accounts;
};


