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

batch_code: {
        type: DataTypes.TEXT,
      
      

      },

manufactured_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

quantity_on_hand: {
        type: DataTypes.DECIMAL,
      
      

      },

unit_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  inventory_batches.associate = (db) => {


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
















    db.inventory_batches.hasMany(db.inventory_transactions, {
      as: 'inventory_transactions_batch',
      foreignKey: {
          name: 'batchId',
      },
      constraints: false,
    });



    db.inventory_batches.hasMany(db.stock_adjustment_lines, {
      as: 'stock_adjustment_lines_batch',
      foreignKey: {
          name: 'batchId',
      },
      constraints: false,
    });



    db.inventory_batches.hasMany(db.transfer_lines, {
      as: 'transfer_lines_batch',
      foreignKey: {
          name: 'batchId',
      },
      constraints: false,
    });














    db.inventory_batches.hasMany(db.sale_lines, {
      as: 'sale_lines_batch',
      foreignKey: {
          name: 'batchId',
      },
      constraints: false,
    });


























//end loop



    db.inventory_batches.belongsTo(db.products, {
      as: 'product',
      foreignKey: {
        name: 'productId',
      },
      constraints: false,
    });

    db.inventory_batches.belongsTo(db.warehouses, {
      as: 'warehouse',
      foreignKey: {
        name: 'warehouseId',
      },
      constraints: false,
    });

    db.inventory_batches.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return inventory_batches;
};


