#ifndef EDITSCENE_ITEMSTATEREGISTRY_HPP
#define EDITSCENE_ITEMSTATEREGISTRY_HPP
#pragma once

#include <nlohmann/json.hpp>
#include <string>
#include <unordered_map>

/**
 * Global item state registry.
 *
 * Tracks per-instance item state keyed by instanceId.
 * Used to persist whether world items have been picked up / disabled
 * across scene reloads and game sessions.
 *
 * Saves to item_state.json.
 */
class ItemStateRegistry {
public:
	static ItemStateRegistry &getInstance();

	struct ItemState {
		std::string instanceId;
		bool disabled = false;
	};

	void setDisabled(const std::string &instanceId, bool disabled);
	bool isDisabled(const std::string &instanceId) const;
	void clearState(const std::string &instanceId);

	nlohmann::json serialize() const;
	void deserialize(const nlohmann::json &j);

	bool saveToFile(const std::string &filepath);
	bool loadFromFile(const std::string &filepath);
	const std::string &getLastError() const
	{
		return m_lastError;
	}

	void autoSave();

private:
	ItemStateRegistry();
	~ItemStateRegistry() = default;

	ItemStateRegistry(const ItemStateRegistry &) = delete;
	ItemStateRegistry &operator=(const ItemStateRegistry &) = delete;

	std::unordered_map<std::string, ItemState> m_states;
	mutable std::string m_lastError;
	std::string m_autoSavePath;
};

#endif // EDITSCENE_ITEMSTATEREGISTRY_HPP
