#ifndef EDITSCENE_CONTAINERSTATEREGISTRY_HPP
#define EDITSCENE_CONTAINERSTATEREGISTRY_HPP
#pragma once

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

/**
 * Global container state registry.
 *
 * Stores runtime container contents keyed by containerId.
 * Used to override scene initial contents for persistent containers.
 * Saves to container_state.json.
 */
class ContainerStateRegistry {
public:
	static ContainerStateRegistry &getInstance();

	struct ContainerSlot {
		std::string itemId;
		int stackSize = 0;
	};

	struct ContainerState {
		std::string containerId;
		std::vector<ContainerSlot> slots;
	};

	void loadState(const std::string &containerId,
		       const std::vector<ContainerSlot> &slots);
	std::vector<ContainerSlot> getState(const std::string &containerId) const;
	bool hasState(const std::string &containerId) const;
	void clearState(const std::string &containerId);

	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:
	ContainerStateRegistry();
	~ContainerStateRegistry() = default;

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

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

#endif // EDITSCENE_CONTAINERSTATEREGISTRY_HPP
