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,
      
      

      },

genre: {
        type: DataTypes.ENUM,
      
      

        values: [

"puzzle",


"strategy",


"quiz",


"arcade",


"simulation",


"word"

        ],

      },

difficulty: {
        type: DataTypes.ENUM,
      
      

        values: [

"easy",


"medium",


"hard",


"expert"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

game_url: {
        type: DataTypes.TEXT,
      
      

      },

time_limit_seconds: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"archived"

        ],

      },

      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.content_items, {
      as: 'content_items_game',
      foreignKey: {
          name: 'gameId',
      },
      constraints: false,
    });





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


















//end loop



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



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


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

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



  return games;
};


