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

issue_number: {
        type: DataTypes.TEXT,
      
      

      },

quantity_issued: {
        type: DataTypes.DECIMAL,
      
      

      },

issued_at: {
        type: DataTypes.DATE,
      
      

      },

issue_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"manual",


"backflush",


"return"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

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

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

    db.material_issues.belongsTo(db.inventory_lots, {
      as: 'lot',
      foreignKey: {
        name: 'lotId',
      },
      constraints: false,
    });

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

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




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

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



  return material_issues;
};


