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

food_name: {
        type: DataTypes.TEXT,
      
      

      },

brand: {
        type: DataTypes.TEXT,
      
      

      },

barcode: {
        type: DataTypes.TEXT,
      
      

      },

serving_size_g: {
        type: DataTypes.DECIMAL,
      
      

      },

calories: {
        type: DataTypes.INTEGER,
      
      

      },

protein_g: {
        type: DataTypes.DECIMAL,
      
      

      },

carbs_g: {
        type: DataTypes.DECIMAL,
      
      

      },

sugars_g: {
        type: DataTypes.DECIMAL,
      
      

      },

fat_g: {
        type: DataTypes.DECIMAL,
      
      

      },

saturated_fat_g: {
        type: DataTypes.DECIMAL,
      
      

      },

fibre_g: {
        type: DataTypes.DECIMAL,
      
      

      },

sodium_mg: {
        type: DataTypes.DECIMAL,
      
      

      },

micronutrients_json: {
        type: DataTypes.TEXT,
      
      

      },

fodmap_tag: {
        type: DataTypes.ENUM,
      
      

        values: [

"unknown",


"low_fodmap",


"high_fodmap"

        ],

      },

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

  foods.associate = (db) => {


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



































    db.foods.hasMany(db.meal_items, {
      as: 'meal_items_food',
      foreignKey: {
          name: 'foodId',
      },
      constraints: false,
    });
















































































//end loop






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

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



  return foods;
};


