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

broker: {
        type: DataTypes.ENUM,
      
      

        values: [

"alpaca",


"interactive_brokers",


"tradingview",


"metatrader4",


"metatrader5",


"binance",


"coinbase",


"kraken",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"connected",


"disconnected",


"error",


"pending"

        ],

      },

connection_label: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

last_sync_at: {
        type: DataTypes.DATE,
      
      

      },

last_error: {
        type: DataTypes.TEXT,
      
      

      },

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

  broker_connections.associate = (db) => {


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























    db.broker_connections.hasMany(db.trade_sync_runs, {
      as: 'trade_sync_runs_broker_connection',
      foreignKey: {
          name: 'broker_connectionId',
      },
      constraints: false,
    });









//end loop



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




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

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



  return broker_connections;
};


