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

code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"yard",


"warehouse",


"shop",


"job_site",


"vehicle"

        ],

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  inventory_locations.associate = (db) => {


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
















    db.inventory_locations.hasMany(db.inventory_items, {
      as: 'inventory_items_location',
      foreignKey: {
          name: 'locationId',
      },
      constraints: false,
    });


    db.inventory_locations.hasMany(db.inventory_transactions, {
      as: 'inventory_transactions_from_location',
      foreignKey: {
          name: 'from_locationId',
      },
      constraints: false,
    });

    db.inventory_locations.hasMany(db.inventory_transactions, {
      as: 'inventory_transactions_to_location',
      foreignKey: {
          name: 'to_locationId',
      },
      constraints: false,
    });






    db.inventory_locations.hasMany(db.goods_receipts, {
      as: 'goods_receipts_received_to_location',
      foreignKey: {
          name: 'received_to_locationId',
      },
      constraints: false,
    });











//end loop






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

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



  return inventory_locations;
};


