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

update_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"stock_in",


"stock_out",


"adjustment",


"audit"

        ],

      },

quantity_change: {
        type: DataTypes.INTEGER,
      
      

      },

quantity_after: {
        type: DataTypes.INTEGER,
      
      

      },

reason: {
        type: DataTypes.TEXT,
      
      

      },

updated_on: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  inventory_updates.associate = (db) => {


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



























//end loop



    db.inventory_updates.belongsTo(db.rural_doctors, {
      as: 'rural_doctor',
      foreignKey: {
        name: 'rural_doctorId',
      },
      constraints: false,
    });

    db.inventory_updates.belongsTo(db.medicines, {
      as: 'medicine',
      foreignKey: {
        name: 'medicineId',
      },
      constraints: false,
    });




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

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



  return inventory_updates;
};


