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

display_name: {
        type: DataTypes.TEXT,
      
      

      },

player_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"joined",


"ready",


"playing",


"disconnected",


"finished"

        ],

      },

score_points: {
        type: DataTypes.INTEGER,
      
      

      },

correct_count: {
        type: DataTypes.INTEGER,
      
      

      },

incorrect_count: {
        type: DataTypes.INTEGER,
      
      

      },

yux_earned: {
        type: DataTypes.INTEGER,
      
      

      },

race_distance_km: {
        type: DataTypes.DECIMAL,
      
      

      },

joined_at: {
        type: DataTypes.DATE,
      
      

      },

left_at: {
        type: DataTypes.DATE,
      
      

      },

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

  match_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.match_players.hasMany(db.player_answers, {
      as: 'player_answers_match_player',
      foreignKey: {
          name: 'match_playerId',
      },
      constraints: false,
    });












//end loop



    db.match_players.belongsTo(db.matches, {
      as: 'match',
      foreignKey: {
        name: 'matchId',
      },
      constraints: false,
    });

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

    db.match_players.belongsTo(db.avatar_customizations, {
      as: 'avatar_customization',
      foreignKey: {
        name: 'avatar_customizationId',
      },
      constraints: false,
    });




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

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



  return match_players;
};


