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

title: {
        type: DataTypes.TEXT,
      
      

      },

game_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"pickup",


"league"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"upcoming",


"in_progress",


"completed",


"cancelled"

        ],

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

target_headcount: {
        type: DataTypes.INTEGER,
      
      

      },

team_count: {
        type: DataTypes.INTEGER,
      
      

      },

field_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

finalized_at: {
        type: DataTypes.DATE,
      
      

      },

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

  games.associate = (db) => {


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















    db.games.hasMany(db.game_rsvps, {
      as: 'game_rsvps_game',
      foreignKey: {
          name: 'gameId',
      },
      constraints: false,
    });


    db.games.hasMany(db.game_guests, {
      as: 'game_guests_game',
      foreignKey: {
          name: 'gameId',
      },
      constraints: false,
    });


    db.games.hasMany(db.game_teams, {
      as: 'game_teams_game',
      foreignKey: {
          name: 'gameId',
      },
      constraints: false,
    });



    db.games.hasMany(db.game_attendances, {
      as: 'game_attendances_game',
      foreignKey: {
          name: 'gameId',
      },
      constraints: false,
    });


    db.games.hasMany(db.game_player_stats, {
      as: 'game_player_stats_game',
      foreignKey: {
          name: 'gameId',
      },
      constraints: false,
    });


    db.games.hasMany(db.game_payments, {
      as: 'game_payments_game',
      foreignKey: {
          name: 'gameId',
      },
      constraints: false,
    });








//end loop



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

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

    db.games.belongsTo(db.venues, {
      as: 'venue',
      foreignKey: {
        name: 'venueId',
      },
      constraints: false,
    });

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




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

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



  return games;
};


