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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"ended",


"archived"

        ],

      },

challenge_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"read_books",


"read_minutes",


"scan_books",


"complete_quizzes",


"earn_coins"

        ],

      },

target_value: {
        type: DataTypes.INTEGER,
      
      

      },

reward_coins: {
        type: DataTypes.INTEGER,
      
      

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

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

  challenge_definitions.associate = (db) => {


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























    db.challenge_definitions.hasMany(db.user_challenges, {
      as: 'user_challenges_challenge_definition',
      foreignKey: {
          name: 'challenge_definitionId',
      },
      constraints: false,
    });

















//end loop






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

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



  return challenge_definitions;
};


