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

name: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"nourriture",


"boisson",


"produit_entretien",


"autre"

        ],

      },

unit: {
        type: DataTypes.TEXT,
      
      

      },

quantity_on_hand: {
        type: DataTypes.DECIMAL,
      
      

      },

reorder_threshold: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  stock_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.stock_items.hasMany(db.stock_movements, {
      as: 'stock_movements_stock_item',
      foreignKey: {
          name: 'stock_itemId',
      },
      constraints: false,
    });





//end loop



    db.stock_items.belongsTo(db.stock_locations, {
      as: 'stock_location',
      foreignKey: {
        name: 'stock_locationId',
      },
      constraints: false,
    });




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

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



  return stock_items;
};


