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

display_name: {
        type: DataTypes.TEXT,
      
      

      },

seed_tier: {
        type: DataTypes.ENUM,
      
      

        values: [

"tier_1_legend",


"tier_2_pro",


"tier_3_elite",


"tier_4_good",


"tier_5_beginner"

        ],

      },

preferred_positions: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  players.associate = (db) => {


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









    db.players.hasMany(db.seed_votes, {
      as: 'seed_votes_target_player',
      foreignKey: {
          name: 'target_playerId',
      },
      constraints: false,
    });





    db.players.hasMany(db.league_team_players, {
      as: 'league_team_players_player',
      foreignKey: {
          name: 'playerId',
      },
      constraints: false,
    });



    db.players.hasMany(db.game_rsvps, {
      as: 'game_rsvps_player',
      foreignKey: {
          name: 'playerId',
      },
      constraints: false,
    });




    db.players.hasMany(db.game_team_assignments, {
      as: 'game_team_assignments_player',
      foreignKey: {
          name: 'playerId',
      },
      constraints: false,
    });


    db.players.hasMany(db.game_attendances, {
      as: 'game_attendances_player',
      foreignKey: {
          name: 'playerId',
      },
      constraints: false,
    });


    db.players.hasMany(db.game_player_stats, {
      as: 'game_player_stats_player',
      foreignKey: {
          name: 'playerId',
      },
      constraints: false,
    });


    db.players.hasMany(db.game_payments, {
      as: 'game_payments_player',
      foreignKey: {
          name: 'playerId',
      },
      constraints: false,
    });


    db.players.hasMany(db.league_payments, {
      as: 'league_payments_player',
      foreignKey: {
          name: 'playerId',
      },
      constraints: false,
    });



    db.players.hasMany(db.league_finish_awards, {
      as: 'league_finish_awards_player',
      foreignKey: {
          name: 'playerId',
      },
      constraints: false,
    });





//end loop



    db.players.belongsTo(db.clubs, {
      as: 'club',
      foreignKey: {
        name: 'clubId',
      },
      constraints: false,
    });

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

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



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


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

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



  return players;
};


