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

location_name: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

timezone: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  tenant_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.tenant_locations.hasMany(db.ponds, {
      as: 'ponds_location',
      foreignKey: {
          name: 'locationId',
      },
      constraints: false,
    });





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







    db.tenant_locations.hasMany(db.marketplace_listings, {
      as: 'marketplace_listings_fulfillment_location',
      foreignKey: {
          name: 'fulfillment_locationId',
      },
      constraints: false,
    });














//end loop



    db.tenant_locations.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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




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

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



  return tenant_locations;
};


