#ifndef EDITSCENE_CHARACTER_CLASS_SYSTEM_HPP
#define EDITSCENE_CHARACTER_CLASS_SYSTEM_HPP
#pragma once

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

class EditorApp;

/**
 * System that manages character progression, need accumulation,
 * and level-up logic.
 *
 * - Accumulates needs each frame based on class database rates.
 * - Sets/clears GOAP blackboard bits when needs cross thresholds.
 * - Checks XP and triggers level-ups.
 * - AI entities auto-distribute stat points.
 * - Player entities get a level-up dialog.
 */
class CharacterClassSystem {
public:
	CharacterClassSystem(flecs::world &world, EditorApp *editorApp);
	~CharacterClassSystem();

	/** Call every frame. Handles need ticks, level-up checks, dialog. */
	void update(float deltaTime);

	/** Render level-up and character-sheet dialogs (inside ImGui frame). */
	void renderDialogs();

	/** Manually add XP to an entity. Returns true if a level up occurred. */
	bool addXP(flecs::entity entity, int64_t amount);

	/** Distribute one point to a stat (player manual or scripted). */
	bool distributePoint(flecs::entity entity, const Ogre::String &statName);

	/** Compute initial stats/skills/needs from class + overrides. */
	static void initializeFromClass(flecs::entity entity);

private:
	void accumulateNeeds(float deltaTime);
	void updateNeedBits(flecs::entity entity);
	void checkLevelUps();
	void applyLevelUp(flecs::entity entity);
	static void applyLevelUpGrowthAndPoints(flecs::entity entity);
	static void distributePointsAI(flecs::entity entity);
	void renderLevelUpDialog(flecs::entity entity);
	void renderCharacterSheet(flecs::entity entity);

	flecs::world &m_world;
	EditorApp *m_editorApp;

	// Track which entities have an open level-up dialog
	std::unordered_set<flecs::entity_t> m_levelUpDialogs;
	// Track which entities have an open character sheet
	std::unordered_set<flecs::entity_t> m_sheets;
};

#endif // EDITSCENE_CHARACTER_CLASS_SYSTEM_HPP
