#ifndef NORMAL_DEBUG_SYSTEM_HPP
#define NORMAL_DEBUG_SYSTEM_HPP

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

class CellGridSystem;

/**
 * Debug visualization system for polygon normals.
 *
 * Draws normal lines for plaza (DistrictComponent) and lot base
 * (LotComponent) geometry using ManualObject with a see-through
 * overlay material (Debug/NormalOverlay).
 *
 * Disabled by default. Toggle via EditorUISystem Settings menu.
 */
class NormalDebugSystem {
public:
	NormalDebugSystem(flecs::world &world, Ogre::SceneManager *sceneMgr,
			  CellGridSystem *cellGridSystem);
	~NormalDebugSystem();

	/**
	 * Update normal visualization each frame.
	 * Rebuilds ManualObject if mesh data has changed.
	 */
	void update();

	/**
	 * Enable/disable normal visualization.
	 */
	void setEnabled(bool enabled);
	bool isEnabled() const
	{
		return m_enabled;
	}

private:
	/**
	 * Rebuild all normal visualizations from scratch.
	 */
	void rebuildAll();

	/**
	 * Draw normals for a single mesh entity.
	 * @param entity The Ogre::Entity whose mesh normals to visualize.
	 * @param parentNode The scene node the entity is attached to (for world-space transform).
	 * @param manualObj The ManualObject to draw into (must be in begin/end scope).
	 */
	void drawMeshNormals(Ogre::Entity *entity, Ogre::SceneNode *parentNode,
			     Ogre::ManualObject *manualObj);

	flecs::world &m_world;
	Ogre::SceneManager *m_sceneMgr;
	CellGridSystem *m_cellGridSystem;

	bool m_enabled = false;

	// ManualObject for normal lines
	Ogre::ManualObject *m_manualObj = nullptr;
	Ogre::SceneNode *m_node = nullptr;

	// Cached entity IDs to detect changes
	std::vector<uint64_t> m_cachedPlazaEntities;
	std::vector<uint64_t> m_cachedLotEntities;
};

#endif // NORMAL_DEBUG_SYSTEM_HPP
