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

challenge_name: {
        type: DataTypes.TEXT,
      
      

      },

challenge_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"individual",


"team"

        ],

      },

metric_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"mission_completions",


"points",


"coins",


"kpi_value",


"quiz_score_average"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

target_value: {
        type: DataTypes.DECIMAL,
      
      

      },

reward_points: {
        type: DataTypes.DECIMAL,
      
      

      },

reward_coins: {
        type: DataTypes.DECIMAL,
      
      

      },

starts_at: {
        type: DataTypes.DATE,
      
      

      },

ends_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"scheduled",


"active",


"completed",


"cancelled"

        ],

      },

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

  challenges.associate = (db) => {


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































    db.challenges.hasMany(db.challenge_participants, {
      as: 'challenge_participants_challenge',
      foreignKey: {
          name: 'challengeId',
      },
      constraints: false,
    });

















//end loop



    db.challenges.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.challenges.belongsTo(db.users, {
      as: 'created_by',
      foreignKey: {
        name: 'created_byId',
      },
      constraints: false,
    });




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

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



  return challenges;
};


