#include "../../include/WorldGenerationFunctions/WorldGenerationFunctionFactory.h"
#include "../../include/WorldGenerationFunctions/IWorldGenerationFunction.h"
#include <algorithm>

// Lazy includes for concrete function implementations
// These will be added as functions are created:
// #include "MeteoriteImpactFunction.h"
// #include "VolcanicActivityFunction.h"
// #include "CraterGenerationFunction.h"
// #include "BasicCoolingFunction.h"

namespace warfactory {

std::unique_ptr<IWorldGenerationFunction> WorldGenerationFunctionFactory::createFunction(const std::string& function_name) {
    const std::unordered_map<std::string, std::function<std::unique_ptr<IWorldGenerationFunction>()>>::const_iterator it = factory_functions.find(function_name);

    if (it != factory_functions.end()) {
        return it->second();
    }

    return nullptr;
}

void WorldGenerationFunctionFactory::registerFunction(const std::string& function_name,
                                                      std::function<std::unique_ptr<IWorldGenerationFunction>()> factory_func) {
    factory_functions[function_name] = factory_func;
}

bool WorldGenerationFunctionFactory::isRegistered(const std::string& function_name) const {
    return factory_functions.find(function_name) != factory_functions.end();
}

std::vector<std::string> WorldGenerationFunctionFactory::getRegisteredFunctions() const {
    std::vector<std::string> function_names;
    function_names.reserve(factory_functions.size());

    for (const std::pair<const std::string, std::function<std::unique_ptr<IWorldGenerationFunction>()>>& pair : factory_functions) {
        function_names.push_back(pair.first);
    }

    // Sort for consistent ordering
    std::sort(function_names.begin(), function_names.end());

    return function_names;
}

void WorldGenerationFunctionFactory::clear() {
    factory_functions.clear();
}

size_t WorldGenerationFunctionFactory::getRegisteredCount() const {
    return factory_functions.size();
}

void WorldGenerationFunctionFactory::registerStandardFunctions() {
    // Standard Phase 1 functions from Regular_world.json
    // These will be implemented progressively:

    // TODO: Implement these function classes
    // registerFunctionType<MeteoriteImpactGenerationFunction>("meteorite_impact_generation");
    // registerFunctionType<ImpactEffectsApplicationFunction>("impact_effects_application");
    // registerFunctionType<MaterialDepositionFunction>("material_deposition");
    // registerFunctionType<PlanetaryCoolingFunction>("planetary_cooling");
    // registerFunctionType<VolcanicActivitySimulationFunction>("volcanic_activity_simulation");
    // registerFunctionType<SurfaceStabilizationFunction>("surface_stabilization");
}

} // namespace warfactory