#ifndef EDITSCENE_ITEM_SYSTEM_HPP
#define EDITSCENE_ITEM_SYSTEM_HPP
#pragma once

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

class EditorApp;
class BehaviorTreeSystem;

/**
 * System that handles item pickup, drop, use, and inventory management.
 *
 * Provides a pure API for inventory operations. All item properties
 * are looked up from ItemRegistry by itemId.
 */
class ItemSystem {
public:
	ItemSystem(flecs::world &world, Ogre::SceneManager *sceneMgr,
		   EditorApp *editorApp, BehaviorTreeSystem *btSystem);
	~ItemSystem();

	// --- Inventory manipulation API ---

	/** Add an item to an inventory by itemId. Creates a new slot. */
	bool addItemToInventory(flecs::entity inventoryEntity,
				const std::string &itemId, int stackSize = 1);

	/** Add an item entity (ItemComponent) to an inventory. */
	bool addItemEntityToInventory(flecs::entity inventoryEntity,
				      flecs::entity itemEntity);

	/** Remove items from inventory by itemId. Returns number removed. */
	int removeItemFromInventory(flecs::entity inventoryEntity,
				    const std::string &itemId, int count = 1);

	/** Remove items from inventory by slot index. */
	bool removeItemFromSlot(flecs::entity inventoryEntity, int slotIndex,
				int count = 1);

	/** Transfer items between two inventories. */
	bool transferItem(flecs::entity fromInventory, int slotIndex,
			  flecs::entity toInventory, int count = 1);

	/** Check if an inventory has at least one of a specific itemId. */
	bool hasItem(flecs::entity inventoryEntity,
		     const std::string &itemId) const;

	/** Check if an inventory has an item with the given display name. */
	bool hasItemByName(flecs::entity inventoryEntity,
			   const std::string &itemName) const;

	/** Count how many of a specific itemId are in an inventory. */
	int countItem(flecs::entity inventoryEntity,
		      const std::string &itemId) const;

	/** Drop an item from inventory into the world at a position. */
	bool dropItem(flecs::entity inventoryEntity, int slotIndex,
		      const Ogre::Vector3 &worldPosition, int count = 1);

	/** Use an item from inventory (executes its use action). */
	bool useItem(flecs::entity characterEntity,
		     flecs::entity inventoryEntity, int slotIndex);

	/**
	 * Pick up a world item entity into a character's inventory.
	 * Called by ActuatorSystem when player presses E near an item.
	 * Returns true if the item was picked up.
	 */
	bool pickupItem(flecs::entity characterEntity,
			flecs::entity itemEntity);

private:
	flecs::world &m_world;
	Ogre::SceneManager *m_sceneMgr;
	EditorApp *m_editorApp;
	BehaviorTreeSystem *m_btSystem;
};

#endif // EDITSCENE_ITEM_SYSTEM_HPP
