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

symbol: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

asset_class: {
        type: DataTypes.ENUM,
      
      

        values: [

"stock",


"crypto",


"forex",


"etf",


"index"

        ],

      },

exchange: {
        type: DataTypes.TEXT,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        type: DataTypes.TEXT,
      
      

      },

sector: {
        type: DataTypes.TEXT,
      
      

      },

industry: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

last_refreshed_at: {
        type: DataTypes.DATE,
      
      

      },

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

  market_symbols.associate = (db) => {


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







    db.market_symbols.hasMany(db.symbol_fundamentals, {
      as: 'symbol_fundamentals_market_symbol',
      foreignKey: {
          name: 'market_symbolId',
      },
      constraints: false,
    });


    db.market_symbols.hasMany(db.price_quotes, {
      as: 'price_quotes_market_symbol',
      foreignKey: {
          name: 'market_symbolId',
      },
      constraints: false,
    });


    db.market_symbols.hasMany(db.technical_indicators, {
      as: 'technical_indicators_market_symbol',
      foreignKey: {
          name: 'market_symbolId',
      },
      constraints: false,
    });


    db.market_symbols.hasMany(db.analyst_ratings, {
      as: 'analyst_ratings_market_symbol',
      foreignKey: {
          name: 'market_symbolId',
      },
      constraints: false,
    });


    db.market_symbols.hasMany(db.earnings_reports, {
      as: 'earnings_reports_market_symbol',
      foreignKey: {
          name: 'market_symbolId',
      },
      constraints: false,
    });



    db.market_symbols.hasMany(db.news_article_symbols, {
      as: 'news_article_symbols_market_symbol',
      foreignKey: {
          name: 'market_symbolId',
      },
      constraints: false,
    });




    db.market_symbols.hasMany(db.watchlist_items, {
      as: 'watchlist_items_market_symbol',
      foreignKey: {
          name: 'market_symbolId',
      },
      constraints: false,
    });



    db.market_symbols.hasMany(db.paper_orders, {
      as: 'paper_orders_market_symbol',
      foreignKey: {
          name: 'market_symbolId',
      },
      constraints: false,
    });



    db.market_symbols.hasMany(db.paper_positions, {
      as: 'paper_positions_market_symbol',
      foreignKey: {
          name: 'market_symbolId',
      },
      constraints: false,
    });





    db.market_symbols.hasMany(db.screener_results, {
      as: 'screener_results_market_symbol',
      foreignKey: {
          name: 'market_symbolId',
      },
      constraints: false,
    });






    db.market_symbols.hasMany(db.ir_companies, {
      as: 'ir_companies_market_symbol',
      foreignKey: {
          name: 'market_symbolId',
      },
      constraints: false,
    });









//end loop






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

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



  return market_symbols;
};


