#pragma once
// ── CS2 External C++ WebRadar — Radar Config Store ─────────────────────────
// Thread-safe configuration store for frontend toggles.
// Stores key-value pairs that the WebSocket server shares with the frontend
// via the m_cheat_config field in the broadcast JSON.

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

class RadarConfigStore {
public:
    using ConfigValue = std::variant<bool, int32_t, float, std::string>;

    // Apply a config change from a frontend command
    bool apply(const std::string& key, const nlohmann::json& value) {
        std::lock_guard<std::mutex> lock(m_mutex);

        if (value.is_boolean())       { m_store[key] = value.get<bool>(); return true; }
        if (value.is_number_integer()){ m_store[key] = value.get<int32_t>(); return true; }
        if (value.is_number_float())  { m_store[key] = value.get<float>(); return true; }
        if (value.is_string())        { m_store[key] = value.get<std::string>(); return true; }
        return false;
    }

    // Snapshot all config as a JSON object (for m_cheat_config field)
    nlohmann::json snapshot() const {
        std::lock_guard<std::mutex> lock(m_mutex);
        nlohmann::json j = nlohmann::json::object();
        for (const auto& [key, val] : m_store) {
            std::visit([&](auto&& v) {
                j[key] = v;
            }, val);
        }
        return j;
    }

private:
    mutable std::mutex m_mutex;
    std::unordered_map<std::string, ConfigValue> m_store;
};
