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

game_code: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"lobby",


"in_progress",


"ended",


"cancelled"

        ],

      },

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

      },

max_players: {
        type: DataTypes.INTEGER,
      
      

      },

question_count: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

      },

wheel_event_every_n_questions: {
        type: DataTypes.INTEGER,
      
      

      },

cat_interaction_every_n_questions: {
        type: DataTypes.INTEGER,
      
      

      },

starting_speed_kmh: {
        type: DataTypes.INTEGER,
      
      

      },

race_map: {
        type: DataTypes.ENUM,
      
      

        values: [

"san_francisco_run",


"milky_way_highway",


"ocean_drive",


"theme_park_rush",


"actually_legal_highway_race"

        ],

      },

scheduled_start_at: {
        type: DataTypes.DATE,
      
      

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

ended_at: {
        type: DataTypes.DATE,
      
      

      },

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

  matches.associate = (db) => {


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
























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


    db.matches.hasMany(db.match_questions, {
      as: 'match_questions_match',
      foreignKey: {
          name: 'matchId',
      },
      constraints: false,
    });



    db.matches.hasMany(db.wheel_events, {
      as: 'wheel_events_match',
      foreignKey: {
          name: 'matchId',
      },
      constraints: false,
    });











//end loop



    db.matches.belongsTo(db.users, {
      as: 'host',
      foreignKey: {
        name: 'hostId',
      },
      constraints: false,
    });

    db.matches.belongsTo(db.game_modes, {
      as: 'mode',
      foreignKey: {
        name: 'modeId',
      },
      constraints: false,
    });

    db.matches.belongsTo(db.quizzes, {
      as: 'quiz',
      foreignKey: {
        name: 'quizId',
      },
      constraints: false,
    });




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

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



  return matches;
};


