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,
      
      

      },

location_kind: {
        type: DataTypes.ENUM,
      
      

        values: [

"building",


"poi",


"resource_node",


"water_spot",


"road",


"interior"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

is_locked: {
        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.player_profiles, {
      as: 'player_profiles_home_location',
      foreignKey: {
          name: 'home_locationId',
      },
      constraints: false,
    });






    db.locations.hasMany(db.npcs, {
      as: 'npcs_home_location',
      foreignKey: {
          name: 'home_locationId',
      },
      constraints: false,
    });

    db.locations.hasMany(db.npcs, {
      as: 'npcs_work_location',
      foreignKey: {
          name: 'work_locationId',
      },
      constraints: false,
    });


    db.locations.hasMany(db.families, {
      as: 'families_home_location',
      foreignKey: {
          name: 'home_locationId',
      },
      constraints: false,
    });






    db.locations.hasMany(db.quests, {
      as: 'quests_start_location',
      foreignKey: {
          name: 'start_locationId',
      },
      constraints: false,
    });

    db.locations.hasMany(db.quests, {
      as: 'quests_end_location',
      foreignKey: {
          name: 'end_locationId',
      },
      constraints: false,
    });


    db.locations.hasMany(db.quest_objectives, {
      as: 'quest_objectives_target_location',
      foreignKey: {
          name: 'target_locationId',
      },
      constraints: false,
    });


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



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







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











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




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



    db.locations.hasMany(db.crafting_recipes, {
      as: 'crafting_recipes_required_station_location',
      foreignKey: {
          name: 'required_station_locationId',
      },
      constraints: false,
    });



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



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





//end loop



    db.locations.belongsTo(db.world_regions, {
      as: 'region',
      foreignKey: {
        name: 'regionId',
      },
      constraints: false,
    });



    db.locations.hasMany(db.file, {
      as: 'location_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.locations.getTableName(),
        belongsToColumn: 'location_images',
      },
    });


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

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



  return locations;
};


