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

recipe_name: {
        type: DataTypes.TEXT,
      
      

      },

prep_time_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

cook_time_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

servings: {
        type: DataTypes.INTEGER,
      
      

      },

difficulty: {
        type: DataTypes.ENUM,
      
      

        values: [

"beginner",


"intermediate",


"chef"

        ],

      },

tags_csv: {
        type: DataTypes.TEXT,
      
      

      },

ingredients_text: {
        type: DataTypes.TEXT,
      
      

      },

instructions_text: {
        type: DataTypes.TEXT,
      
      

      },

nutrition_per_serving_json: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

rating: {
        type: DataTypes.DECIMAL,
      
      

      },

source_url: {
        type: DataTypes.TEXT,
      
      

      },

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

  recipes.associate = (db) => {


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



































    db.recipes.hasMany(db.meal_items, {
      as: 'meal_items_recipe',
      foreignKey: {
          name: 'recipeId',
      },
      constraints: false,
    });




    db.recipes.hasMany(db.meal_plan_items, {
      as: 'meal_plan_items_recipe',
      foreignKey: {
          name: 'recipeId',
      },
      constraints: false,
    });













































































//end loop



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



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


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

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



  return recipes;
};


