#pragma once

#include "../WorldGenerationFunctions/IWorldGenerationFunction.h"
#include "../WorldData.h"
#include "../PlanetaryCore.h"
#include "../RegionManager.h"
#include "warfactory/RandomGenerator.h"

namespace warfactory {

/**
 * @brief Core-driven volcanic activity and material redistribution
 *
 * Simulates volcanic overflow when core pressure exceeds threshold.
 * Redistributes materials from planetary core to surface through volcanic activity,
 * creating volcanic regions and transferring heavy metals upward.
 */
class VolcanicRedistributionFunction : public IWorldGenerationFunction {
private:
    // Configuration parameters
    float core_pressure_threshold;        // Core pressure ratio to trigger volcanism
    float volcanic_overflow_rate;         // Rate of material overflow per cycle
    float core_to_surface_redistribution; // Fraction of core material redistributed

public:
    VolcanicRedistributionFunction();

    // Inherited from IWorldGenerationFunction
    void configure(const IDataNode& config) override;
    void execute(WorldData& world, PlanetaryCore& core) override;
    std::string getStepName() const override;
    bool isConfigured() const override;
    void reset() override;

private:
    /**
     * @brief Check if core pressure exceeds volcanic threshold
     */
    bool shouldTriggerVolcanicActivity(const PlanetaryCore& core) const;

    /**
     * @brief Calculate current core pressure ratio (0.0 = empty, 1.0 = full)
     */
    float calculateCorePressureRatio(const PlanetaryCore& core) const;

    /**
     * @brief Create volcanic eruption sites on world surface
     */
    void createVolcanicEruptions(WorldData& world, const PlanetaryCore& core);

    /**
     * @brief Transfer materials from core to surface through volcanism
     */
    void redistributeCoreMaterials(PlanetaryCore& core, RegionManager& region_manager);

    /**
     * @brief Generate random volcanic eruption site
     */
    void generateVolcanicSite(float& x, float& y, float& intensity) const;

    /**
     * @brief Apply volcanic heat to terrain
     */
    void applyVolcanicHeat(WorldData& world, float x, float y, float heat_intensity);

    /**
     * @brief Calculate material overflow quantity from core
     */
    __uint128_t calculateOverflowQuantity(const PlanetaryCore& core, uint32_t resource_id) const;
};

} // namespace warfactory