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

placed_at: {
        type: DataTypes.DATE,
      
      

      },

filled_at: {
        type: DataTypes.DATE,
      
      

      },

side: {
        type: DataTypes.ENUM,
      
      

        values: [

"buy",


"sell"

        ],

      },

order_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"market",


"limit",


"stop",


"stop_limit"

        ],

      },

quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

limit_price: {
        type: DataTypes.DECIMAL,
      
      

      },

stop_price: {
        type: DataTypes.DECIMAL,
      
      

      },

filled_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

average_fill_price: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"placed",


"partially_filled",


"filled",


"canceled",


"rejected",


"expired"

        ],

      },

rejection_reason: {
        type: DataTypes.TEXT,
      
      

      },

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

  paper_orders.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_orders.hasMany(db.paper_trades, {
      as: 'paper_trades_paper_order',
      foreignKey: {
          name: 'paper_orderId',
      },
      constraints: false,
    });



















//end loop



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

    db.paper_orders.belongsTo(db.market_symbols, {
      as: 'market_symbol',
      foreignKey: {
        name: 'market_symbolId',
      },
      constraints: false,
    });




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

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



  return paper_orders;
};


