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

issue_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"manual",


"backflush"

        ],

      },

quantity_required: {
        type: DataTypes.DECIMAL,
      
      

      },

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

    db.production_materials.belongsTo(db.items, {
      as: 'component_item',
      foreignKey: {
        name: 'component_itemId',
      },
      constraints: false,
    });

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

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

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




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

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



  return production_materials;
};


