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

required_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

issued_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

issue_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"manual",


"backflush"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"partially_issued",


"fully_issued",


"short"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

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

    db.material_requirements.belongsTo(db.items, {
      as: 'item',
      foreignKey: {
        name: 'itemId',
      },
      constraints: false,
    });

    db.material_requirements.belongsTo(db.uoms, {
      as: 'uom',
      foreignKey: {
        name: 'uomId',
      },
      constraints: false,
    });




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

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



  return material_requirements;
};


