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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

item_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"nourriture",


"boisson"

        ],

      },

price: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

prep_time_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

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

  menu_items.associate = (db) => {


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















    db.menu_items.hasMany(db.order_items, {
      as: 'order_items_menu_item',
      foreignKey: {
          name: 'menu_itemId',
      },
      constraints: false,
    });















//end loop



    db.menu_items.belongsTo(db.restaurants, {
      as: 'restaurant',
      foreignKey: {
        name: 'restaurantId',
      },
      constraints: false,
    });

    db.menu_items.belongsTo(db.menu_categories, {
      as: 'menu_category',
      foreignKey: {
        name: 'menu_categoryId',
      },
      constraints: false,
    });



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


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

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



  return menu_items;
};


