#pragma once

#include <flecs.h>
#include <memory>
#include <Ogre.h>

class StaticGeometrySystem {
public:
    StaticGeometrySystem(flecs::world& world, Ogre::SceneManager* sceneMgr);
    ~StaticGeometrySystem();
    
    // Initialize the system
    void initialize();
    
    // Main update - processes dirty regions and rebuilds
    void update();
    
    // Force rebuild of a specific region
    void rebuildRegion(flecs::entity regionEntity);
    
    // Destroy a region's StaticGeometry
    void destroyRegion(flecs::entity regionEntity);
    
    // Get the Ogre::StaticGeometry for a region (if built)
    Ogre::StaticGeometry* getStaticGeometry(flecs::entity regionEntity);
    
    // Check if a region needs rebuild
    bool isRegionDirty(flecs::entity regionEntity);
    
    // Get member count for a region
    std::size_t getMemberCount(flecs::entity regionEntity);
    
private:
    flecs::world& m_world;
    Ogre::SceneManager* m_sceneMgr;
    
    // Queries
    flecs::query<struct StaticGeometryComponent> m_regionQuery;
    flecs::query<struct StaticGeometryMemberComponent> m_memberQuery;
    
    bool m_initialized = false;
    
    // Build a region's StaticGeometry
    void buildRegion(flecs::entity regionEntity, struct StaticGeometryComponent& region);
    
    // Destroy Ogre StaticGeometry object
    void destroyOgreStaticGeometry(struct StaticGeometryComponent& region);
    
    // Check if any members are dirty
    bool areMembersDirty(flecs::entity regionEntity);
    
    // Validate region exists and is valid
    bool isValidRegion(flecs::entity regionEntity);
};
