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

rule_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"overtrading",


"revenge_trading",


"drawdown",


"streak_loss",


"streak_win",


"risk_spike"

        ],

      },

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

      },

threshold_value: {
        type: DataTypes.DECIMAL,
      
      

      },

threshold_unit: {
        type: DataTypes.ENUM,
      
      

        values: [

"count",


"percent",


"currency",


"rr",


"minutes",


"days"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  alert_rules.associate = (db) => {


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



























    db.alert_rules.hasMany(db.alerts, {
      as: 'alerts_alert_rule',
      foreignKey: {
          name: 'alert_ruleId',
      },
      constraints: false,
    });





//end loop



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




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

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



  return alert_rules;
};


