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

logged_at: {
        type: DataTypes.DATE,
      
      

      },

meal_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"breakfast",


"morning_snack",


"lunch",


"afternoon_snack",


"dinner",


"evening_snack",


"other"

        ],

      },

meal_title: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  meals.associate = (db) => {


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



































    db.meals.hasMany(db.meal_items, {
      as: 'meal_items_meal',
      foreignKey: {
          name: 'mealId',
      },
      constraints: false,
    });
















































































//end loop



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



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

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


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

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



  return meals;
};


