#pragma once

#include "IModule.h"
#include "GMap.h"
#include <nlohmann/json.hpp>
#include <memory>
#include <string>

using json = nlohmann::json;

namespace warfactory {

/**
 * @brief World generation module for creating the global world map
 *
 * This module generates the complete world map using procedural algorithms.
 * It handles terrain generation, biome placement, elevation calculation,
 * and geographical feature placement.
 *
 * Configuration is loaded from gameData/:
 * - WorldGeneration/ - Generation algorithms and parameters
 * - Biomes/ - Biome definitions and properties
 * - Terrain/ - Terrain type definitions
 * - MapFeatures/ - Geographical features
 */
class WorldGenerationModule : public IModule {
private:
    std::unique_ptr<GMap> worldMap;
    uint64_t generationSeed;
    bool isWorldGenerated;

    /**
     * @brief Load generation parameters from configuration
     */
    void loadGenerationConfig(const IDataNode& config);

    /**
     * @brief Generate terrain using procedural algorithms
     */
    void generateTerrain();

    /**
     * @brief Apply biomes based on temperature and humidity
     */
    void generateBiomes();

    /**
     * @brief Calculate elevation using noise functions
     */
    void generateElevation();

    /**
     * @brief Place geographical features (rivers, mountains, etc.)
     */
    void generateFeatures();

public:
    /**
     * @brief Constructor
     */
    WorldGenerationModule();

    /**
     * @brief Destructor
     */
    virtual ~WorldGenerationModule() = default;

    // IModule interface implementation
    void setConfiguration(const IDataNode& configNode, IIO* io, ITaskScheduler* scheduler) override;
    const IDataNode& getConfiguration() override;
    json process(const json& input) override;
    void shutdown() override;
    json getState() override;
    void setState(const json& state) override;
    json getHealthStatus() override;
    std::string getType() const override;

    /**
     * @brief Generate the complete world map
     * @return JSON status of generation
     */
    json generateWorldMap();

    /**
     * @brief Get terrain type at world coordinates
     * @param x World X coordinate
     * @param y World Y coordinate
     * @return Terrain type string
     */
    std::string getTerrainAt(int x, int y) const;

    /**
     * @brief Get biome type at world coordinates
     * @param x World X coordinate
     * @param y World Y coordinate
     * @return Biome type string
     */
    std::string getBiomeAt(int x, int y) const;

    /**
     * @brief Get elevation at world coordinates
     * @param x World X coordinate
     * @param y World Y coordinate
     * @return Elevation value
     */
    float getElevationAt(int x, int y) const;

    /**
     * @brief Get world map dimensions
     * @return JSON with width and height
     */
    json getWorldDimensions() const;

    /**
     * @brief Export region of the world map
     * @param startX Starting X coordinate
     * @param startY Starting Y coordinate
     * @param width Region width
     * @param height Region height
     * @return JSON representation of the region
     */
    json exportRegion(int startX, int startY, int width, int height) const;

    /**
     * @brief Set generation seed for deterministic results
     * @param seed New seed value
     */
    void setSeed(uint64_t seed);

    /**
     * @brief Get current generation seed
     * @return Current seed value
     */
    uint64_t getSeed() const;

    /**
     * @brief Check if world has been generated
     * @return true if world is generated, false otherwise
     */
    bool isGenerated() const;
};

} // namespace warfactory