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

habit_name: {
        type: DataTypes.TEXT,
      
      

      },

icon_name: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"health",


"fitness",


"nutrition",


"mental",


"sleep",


"social",


"personal_growth",


"medical"

        ],

      },

frequency_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"daily",


"times_per_week",


"specific_days",


"every_x_days"

        ],

      },

times_per_week: {
        type: DataTypes.INTEGER,
      
      

      },

specific_days_csv: {
        type: DataTypes.TEXT,
      
      

      },

every_x_days: {
        type: DataTypes.INTEGER,
      
      

      },

target_count_per_day: {
        type: DataTypes.INTEGER,
      
      

      },

target_duration_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

end_date: {
        type: DataTypes.DATE,
      
      

      },

motivation: {
        type: DataTypes.TEXT,
      
      

      },

stacking_note: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  habits.associate = (db) => {


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























































    db.habits.hasMany(db.habit_logs, {
      as: 'habit_logs_habit',
      foreignKey: {
          name: 'habitId',
      },
      constraints: false,
    });




























































//end loop



    db.habits.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });




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

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



  return habits;
};


