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: [

"stock",


"receiving",


"shipping",


"staging",


"returns",


"scrap"

        ],

      },

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

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



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

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






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








    db.locations.hasMany(db.receipts, {
      as: 'receipts_received_into_location',
      foreignKey: {
          name: 'received_into_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;
};


