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

uom_name: {
        type: DataTypes.TEXT,
      
      

      },

uom_code: {
        type: DataTypes.TEXT,
      
      

      },

base_multiplier: {
        type: DataTypes.DECIMAL,
      
      

      },

active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  uoms.associate = (db) => {


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










    db.uoms.hasMany(db.items, {
      as: 'items_default_uom',
      foreignKey: {
          name: 'default_uomId',
      },
      constraints: false,
    });




    db.uoms.hasMany(db.bom_lines, {
      as: 'bom_lines_uom',
      foreignKey: {
          name: 'uomId',
      },
      constraints: false,
    });













    db.uoms.hasMany(db.inventory_movements, {
      as: 'inventory_movements_uom',
      foreignKey: {
          name: 'uomId',
      },
      constraints: false,
    });



    db.uoms.hasMany(db.purchase_order_lines, {
      as: 'purchase_order_lines_uom',
      foreignKey: {
          name: 'uomId',
      },
      constraints: false,
    });











//end loop






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

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



  return uoms;
};


