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

direction: {
        type: DataTypes.ENUM,
      
      

        values: [

"long",


"short"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"open",


"closed",


"canceled"

        ],

      },

timeframe: {
        type: DataTypes.ENUM,
      
      

        values: [

"1m",


"5m",


"15m",


"30m",


"1h",


"4h",


"1d",


"1w",


"1mo",


"custom"

        ],

      },

entry_at: {
        type: DataTypes.DATE,
      
      

      },

exit_at: {
        type: DataTypes.DATE,
      
      

      },

entry_price: {
        type: DataTypes.DECIMAL,
      
      

      },

exit_price: {
        type: DataTypes.DECIMAL,
      
      

      },

stop_loss_price: {
        type: DataTypes.DECIMAL,
      
      

      },

take_profit_price: {
        type: DataTypes.DECIMAL,
      
      

      },

position_size: {
        type: DataTypes.DECIMAL,
      
      

      },

position_size_unit: {
        type: DataTypes.ENUM,
      
      

        values: [

"shares",


"contracts",


"lots",


"units"

        ],

      },

fees: {
        type: DataTypes.DECIMAL,
      
      

      },

slippage: {
        type: DataTypes.DECIMAL,
      
      

      },

pnl_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

pnl_percent: {
        type: DataTypes.DECIMAL,
      
      

      },

risk_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

rr_ratio: {
        type: DataTypes.DECIMAL,
      
      

      },

mae_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

mfe_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

duration_minutes: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  trades.associate = (db) => {

    db.trades.belongsToMany(db.tags, {
      as: 'tags',
      foreignKey: {
        name: 'trades_tagsId',
      },
      constraints: false,
      through: 'tradesTagsTags',
    });

    db.trades.belongsToMany(db.tags, {
      as: 'tags_filter',
      foreignKey: {
        name: 'trades_tagsId',
      },
      constraints: false,
      through: 'tradesTagsTags',
    });


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












    db.trades.hasMany(db.trade_attachments, {
      as: 'trade_attachments_trade',
      foreignKey: {
          name: 'tradeId',
      },
      constraints: false,
    });



    db.trades.hasMany(db.trade_rule_checks, {
      as: 'trade_rule_checks_trade',
      foreignKey: {
          name: 'tradeId',
      },
      constraints: false,
    });


    db.trades.hasMany(db.mood_entries, {
      as: 'mood_entries_trade',
      foreignKey: {
          name: 'tradeId',
      },
      constraints: false,
    });













    db.trades.hasMany(db.alerts, {
      as: 'alerts_trade',
      foreignKey: {
          name: 'tradeId',
      },
      constraints: false,
    });





//end loop



    db.trades.belongsTo(db.trading_accounts, {
      as: 'trading_account',
      foreignKey: {
        name: 'trading_accountId',
      },
      constraints: false,
    });

    db.trades.belongsTo(db.instruments, {
      as: 'instrument',
      foreignKey: {
        name: 'instrumentId',
      },
      constraints: false,
    });

    db.trades.belongsTo(db.setups, {
      as: 'setup',
      foreignKey: {
        name: 'setupId',
      },
      constraints: false,
    });




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

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



  return trades;
};


