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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

location_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"receiving",


"storage",


"wip",


"qa_hold",


"shipping",


"scrap"

        ],

      },

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

      },

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

  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.locations.hasMany(db.inventory_balances, {
      as: 'inventory_balances_location',
      foreignKey: {
          name: 'locationId',
      },
      constraints: false,
    });


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

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



    db.locations.hasMany(db.cycle_count_lines, {
      as: 'cycle_count_lines_location',
      foreignKey: {
          name: 'locationId',
      },
      constraints: false,
    });




    db.locations.hasMany(db.material_allocations, {
      as: 'material_allocations_from_location',
      foreignKey: {
          name: 'from_locationId',
      },
      constraints: false,
    });















    db.locations.hasMany(db.shipment_lines, {
      as: 'shipment_lines_from_location',
      foreignKey: {
          name: 'from_locationId',
      },
      constraints: false,
    });



//end loop



    db.locations.belongsTo(db.warehouses, {
      as: 'warehouse',
      foreignKey: {
        name: 'warehouseId',
      },
      constraints: false,
    });

    db.locations.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return locations;
};


