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

quantity_issued: {
        type: DataTypes.DECIMAL,
      
      

      },

issued_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  production_material_issues.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.production_material_issues.belongsTo(db.production_orders, {
      as: 'production_order',
      foreignKey: {
        name: 'production_orderId',
      },
      constraints: false,
    });

    db.production_material_issues.belongsTo(db.materials, {
      as: 'material',
      foreignKey: {
        name: 'materialId',
      },
      constraints: false,
    });

    db.production_material_issues.belongsTo(db.inventory_lots, {
      as: 'inventory_lot',
      foreignKey: {
        name: 'inventory_lotId',
      },
      constraints: false,
    });

    db.production_material_issues.belongsTo(db.storage_locations, {
      as: 'from_location',
      foreignKey: {
        name: 'from_locationId',
      },
      constraints: false,
    });

    db.production_material_issues.belongsTo(db.users, {
      as: 'issued_by',
      foreignKey: {
        name: 'issued_byId',
      },
      constraints: false,
    });




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

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



  return production_material_issues;
};


