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

name: {
        type: DataTypes.TEXT,
      
      

      },

color_label: {
        type: DataTypes.TEXT,
      
      

      },

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

  league_teams.associate = (db) => {


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













    db.league_teams.hasMany(db.league_team_players, {
      as: 'league_team_players_league_team',
      foreignKey: {
          name: 'league_teamId',
      },
      constraints: false,
    });











    db.league_teams.hasMany(db.league_standings, {
      as: 'league_standings_league_team',
      foreignKey: {
          name: 'league_teamId',
      },
      constraints: false,
    });






//end loop



    db.league_teams.belongsTo(db.leagues, {
      as: 'league',
      foreignKey: {
        name: 'leagueId',
      },
      constraints: false,
    });

    db.league_teams.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



    db.league_teams.hasMany(db.file, {
      as: 'logo',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.league_teams.getTableName(),
        belongsToColumn: 'logo',
      },
    });


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

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



  return league_teams;
};


