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

title: {
        type: DataTypes.TEXT,
      
      

      },

scope: {
        type: DataTypes.ENUM,
      
      

        values: [

"global",


"country",


"organization",


"course",


"weekly",


"monthly"

        ],

      },

metric: {
        type: DataTypes.ENUM,
      
      

        values: [

"score",


"streak_days",


"completed_items",


"accuracy"

        ],

      },

period_start: {
        type: DataTypes.DATE,
      
      

      },

period_end: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  leaderboards.associate = (db) => {


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

















    db.leaderboards.hasMany(db.leaderboard_entries, {
      as: 'leaderboard_entries_leaderboard',
      foreignKey: {
          name: 'leaderboardId',
      },
      constraints: false,
    });
















//end loop



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

    db.leaderboards.belongsTo(db.courses, {
      as: 'course',
      foreignKey: {
        name: 'courseId',
      },
      constraints: false,
    });




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

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



  return leaderboards;
};


