#include "StaticGeometry.hpp"
#include "StaticGeometryMember.hpp"
#include "../ui/ComponentRegistration.hpp"
#include "../ui/StaticGeometryEditor.hpp"
#include "../ui/StaticGeometryMemberEditor.hpp"
#include <Ogre.h>
#include <random>

// Helper to generate unique ID
static std::string generateRegionId() {
    static std::random_device rd;
    static std::mt19937 gen(rd());
    static std::uniform_int_distribution<> dis(1000, 9999);
    return "region_" + std::to_string(dis(gen));
}

// Register StaticGeometry (region) component
REGISTER_COMPONENT("StaticGeometry Region", StaticGeometryComponent, StaticGeometryEditor)
{
    registry.registerComponent<StaticGeometryComponent>(
        "StaticGeometry Region",
        std::make_unique<StaticGeometryEditor>(),
        // Adder
        [sceneMgr](flecs::entity e) {
            if (!e.has<StaticGeometryComponent>()) {
                auto region = StaticGeometryComponent();
                region.regionId = generateRegionId();
                region.regionName = "Region_" + Ogre::StringConverter::toString(e.id());
                e.set<StaticGeometryComponent>(region);
            }
        },
        // Remover
        [sceneMgr](flecs::entity e) {
            if (e.has<StaticGeometryComponent>()) {
                e.remove<StaticGeometryComponent>();
            }
        }
    );
}

// Register StaticGeometryMember component
REGISTER_COMPONENT("StaticGeometry Member", StaticGeometryMemberComponent, StaticGeometryMemberEditor)
{
    registry.registerComponent<StaticGeometryMemberComponent>(
        "StaticGeometry Member",
        std::make_unique<StaticGeometryMemberEditor>(sceneMgr),
        // Adder
        [sceneMgr](flecs::entity e) {
            if (!e.has<StaticGeometryMemberComponent>()) {
                e.set<StaticGeometryMemberComponent>({});
            }
        },
        // Remover
        [sceneMgr](flecs::entity e) {
            if (e.has<StaticGeometryMemberComponent>()) {
                e.remove<StaticGeometryMemberComponent>();
            }
        }
    );
}
