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

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

goal_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"pnl_amount",


"pnl_percent",


"win_rate",


"profit_factor",


"max_drawdown",


"consistency",


"trade_count",


"rule_compliance",


"sharpe_ratio",


"custom"

        ],

      },

target_value: {
        type: DataTypes.DECIMAL,
      
      

      },

current_value: {
        type: DataTypes.DECIMAL,
      
      

      },

period: {
        type: DataTypes.ENUM,
      
      

        values: [

"weekly",


"monthly",


"quarterly",


"yearly",


"custom"

        ],

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"achieved",


"failed",


"paused",


"canceled"

        ],

      },

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

  goals.associate = (db) => {


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

















    db.goals.hasMany(db.milestones, {
      as: 'milestones_goal',
      foreignKey: {
          name: 'goalId',
      },
      constraints: false,
    });















//end loop



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




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

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



  return goals;
};


