#include "register_types.h"
#include "simulation_server.h"

#include <gdextension_interface.h>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/godot.hpp>

namespace tactical_shooter {

void initialize_simulation_module(godot::ModuleInitializationLevel p_level) {
    if (p_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) {
        return;
    }

    // Register all GDScript-facing classes
    godot::ClassDB::register_class<SimulationServer>();
    godot::ClassDB::register_class<Entity>();
}

void uninitialize_simulation_module(godot::ModuleInitializationLevel p_level) {
    if (p_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) {
        return;
    }
}

} // namespace tactical_shooter

extern "C" {

// GDExtension entry point — called by Godot to initialize the extension
GDExtensionBool GDE_EXPORT gdextension_entry(
    GDExtensionInterfaceGetProcAddress p_get_proc_address,
    GDExtensionClassLibraryPtr p_library,
    GDExtensionInitialization *r_initialization
) {
    godot::GDExtensionBinding::InitObject init_obj(
        p_get_proc_address, p_library, r_initialization
    );

    init_obj.register_initializer(
        tactical_shooter::initialize_simulation_module
    );
    init_obj.register_terminator(
        tactical_shooter::uninitialize_simulation_module
    );
    init_obj.set_minimum_library_initialization_level(
        godot::MODULE_INITIALIZATION_LEVEL_SCENE
    );

    return init_obj.init();
}

} // extern "C"
