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

workout_name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

workout_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"strength_training",


"hiit",


"cardio",


"yoga",


"pilates",


"stretching",


"mobility",


"sport_specific",


"circuit",


"crossfit_style",


"martial_arts",


"dance",


"prenatal",


"postnatal",


"senior_friendly",


"recovery",


"rehab"

        ],

      },

goal_focus: {
        type: DataTypes.ENUM,
      
      

        values: [

"fat_loss",


"muscle_gain",


"endurance",


"flexibility",


"sport_specific",


"recovery",


"rehab"

        ],

      },

difficulty: {
        type: DataTypes.ENUM,
      
      

        values: [

"beginner",


"intermediate",


"advanced",


"elite"

        ],

      },

equipment_needed: {
        type: DataTypes.ENUM,
      
      

        values: [

"none_bodyweight",


"dumbbells",


"barbell",


"resistance_bands",


"gym_machines",


"trx",


"kettlebells",


"swimming_pool",


"bike",


"treadmill",


"rowing_machine",


"other"

        ],

      },

duration_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

estimated_calories: {
        type: DataTypes.INTEGER,
      
      

      },

average_rating: {
        type: DataTypes.DECIMAL,
      
      

      },

warmup_notes: {
        type: DataTypes.TEXT,
      
      

      },

cooldown_notes: {
        type: DataTypes.TEXT,
      
      

      },

safety_notes: {
        type: DataTypes.TEXT,
      
      

      },

video_url: {
        type: DataTypes.TEXT,
      
      

      },

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

  workouts.associate = (db) => {


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






















    db.workouts.hasMany(db.workout_exercises, {
      as: 'workout_exercises_workout',
      foreignKey: {
          name: 'workoutId',
      },
      constraints: false,
    });


    db.workouts.hasMany(db.workout_schedules, {
      as: 'workout_schedules_workout',
      foreignKey: {
          name: 'workoutId',
      },
      constraints: false,
    });



    db.workouts.hasMany(db.workout_sessions, {
      as: 'workout_sessions_workout',
      foreignKey: {
          name: 'workoutId',
      },
      constraints: false,
    });


























































































//end loop





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


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

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



  return workouts;
};


