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

name: {
        type: DataTypes.TEXT,
      
      

      },

broker_name: {
        type: DataTypes.TEXT,
      
      

      },

base_currency: {
        type: DataTypes.TEXT,
      
      

      },

starting_balance: {
        type: DataTypes.DECIMAL,
      
      

      },

current_balance: {
        type: DataTypes.DECIMAL,
      
      

      },

risk_per_trade_default: {
        type: DataTypes.DECIMAL,
      
      

      },

risk_unit: {
        type: DataTypes.ENUM,
      
      

        values: [

"percent",


"currency"

        ],

      },

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

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

archived_at: {
        type: DataTypes.DATE,
      
      

      },

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

  trading_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.trading_accounts.hasMany(db.trades, {
      as: 'trades_trading_account',
      foreignKey: {
          name: 'trading_accountId',
      },
      constraints: false,
    });






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





    db.trading_accounts.hasMany(db.csv_import_jobs, {
      as: 'csv_import_jobs_trading_account',
      foreignKey: {
          name: 'trading_accountId',
      },
      constraints: false,
    });



    db.trading_accounts.hasMany(db.broker_connections, {
      as: 'broker_connections_trading_account',
      foreignKey: {
          name: 'trading_accountId',
      },
      constraints: false,
    });



    db.trading_accounts.hasMany(db.ai_insight_runs, {
      as: 'ai_insight_runs_trading_account',
      foreignKey: {
          name: 'trading_accountId',
      },
      constraints: false,
    });



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




    db.trading_accounts.hasMany(db.pdf_reports, {
      as: 'pdf_reports_trading_account',
      foreignKey: {
          name: 'trading_accountId',
      },
      constraints: false,
    });



//end loop



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




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

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



  return trading_accounts;
};


