#pragma once

#include "../WorldGenerationFunctions/IWorldGenerationFunction.h"
#include "../MeteoriteImpact.h"
#include "../Meteorite.h"
#include "../WorldData.h"
#include "../PlanetaryCore.h"
#include "../RegionManager.h"
#include "warfactory/RandomGenerator.h"
#include <vector>
#include <memory>

namespace warfactory {

/**
 * @brief Apply physical effects of meteorite impacts to the world
 *
 * Consumer in the Producer/Consumer pattern. Takes generated meteorites
 * and applies their physical effects: crater formation, heat generation,
 * and material deposition to the world terrain and planetary core.
 */
class ImpactEffectsApplicationFunction : public IWorldGenerationFunction {
private:
    // Configuration parameters
    float kinetic_energy_to_heat_ratio;
    float crater_formation_factor;
    float surface_metal_deposit_ratio;

    // External meteorites source (from generation function)
    std::vector<std::unique_ptr<Meteorite>> external_meteorites;

public:
    ImpactEffectsApplicationFunction();

    // 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;

    /**
     * @brief Set meteorites to process (from generation function)
     * @param meteorites Vector of meteorites to apply (ownership transferred)
     */
    void setMeteoritesToProcess(std::vector<std::unique_ptr<Meteorite>> meteorites);

private:
    /**
     * @brief Generate random impact parameters for a meteorite
     */
    void generateImpactParameters(const Meteorite& meteorite, float& size, float& velocity,
                                 float& angle, float& x, float& y) const;

    /**
     * @brief Create and apply single meteorite impact
     */
    void applyMeteoriteImpact(const Meteorite& meteorite, WorldData& world,
                             PlanetaryCore& core, RegionManager& region_manager);

    /**
     * @brief Calculate impact size based on meteorite mass and configuration
     */
    float calculateImpactSize(const Meteorite& meteorite) const;

    /**
     * @brief Generate random impact velocity (km/s)
     */
    float generateImpactVelocity() const;

    /**
     * @brief Generate random impact angle (radians)
     */
    float generateImpactAngle() const;

    /**
     * @brief Generate random impact position on world map
     */
    void generateImpactPosition(float& x, float& y) const;
};

} // namespace warfactory