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,
      },

location_name: {
        type: DataTypes.TEXT,
      
      

      },

location_code: {
        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,
    },
  );

  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_movements, {
      as: 'inventory_movements_from_location',
      foreignKey: {
          name: 'from_locationId',
      },
      constraints: false,
    });

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













//end loop



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

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




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

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



  return locations;
};


