/*
 * ============================================================
 *  Ejemplo 05 — Client Features (toggles y sliders)
 *
 *  Endpoint público: GET /api/client_features
 *  Endpoint por categoría + API key: GET /api/cat/{slug}/key/{api_key}
 *
 *  Desde el panel (Funciones) creas toggles y sliders que
 *  tu C++ lee en tiempo real. El usuario del panel los activa/
 *  desactiva y tu cheat los aplica sin recompilar.
 *
 *  Casos de uso:
 *    - Activar/desactivar features del cheat remotamente
 *    - Ajustar valores (FOV, velocidad, etc.) desde el panel
 *    - Diferentes configs por suscripción
 * ============================================================
 */
#include "SaoAuth.hpp"
#include <iostream>
#include <map>

const std::wstring PANEL_HOST = L"saoauth.com";

// ── Estructura de una feature ─────────────────────────────────────────────────
struct Feature {
    std::string name;
    std::string type;    // "toggle" | "slider"
    int         tid;     // toggle_id — tu ID interno para identificarla
    bool        enabled; // para toggles
    float       value;   // para sliders
    float       min_val, max_val, step;
    std::string unit;
};

// ── Cargar todas las features públicas ───────────────────────────────────────
std::map<int, Feature> LoadFeatures() {
    std::string raw = SaoAuth::HttpGet(PANEL_HOST, L"/api/client_features");
    std::map<int, Feature> features;

    if (raw.empty()) return features;

    try {
        auto j = json::parse(raw);
        if (!j.value("success", false)) return features;

        // "flat" es un mapa tid → feature (más fácil de usar en C++)
        for (auto& [tid_str, f] : j["flat"].items()) {
            Feature feat;
            feat.tid     = std::stoi(tid_str);
            feat.name    = f.value("name", "");
            feat.type    = f.value("type", "toggle");
            feat.enabled = f.value("default", 0) != 0;
            feat.value   = f.value("default", 0.0f);
            feat.min_val = f.value("min", 0.0f);
            feat.max_val = f.value("max", 100.0f);
            feat.step    = f.value("step", 1.0f);
            feat.unit    = f.value("unit", "");
            features[feat.tid] = feat;
        }
    } catch (...) {}

    return features;
}

// ── Cargar features de una categoría específica con API key ──────────────────
// La API key la obtienes en el panel: Funciones → (categoría) → API Key
std::map<int, Feature> LoadCategoryFeatures(const std::string& slug,
                                             const std::string& api_key) {
    std::wstring path = L"/api/cat/" +
        std::wstring(slug.begin(), slug.end()) + L"/key/" +
        std::wstring(api_key.begin(), api_key.end());

    std::string raw = SaoAuth::HttpGet(PANEL_HOST, path);
    std::map<int, Feature> features;

    if (raw.empty()) return features;

    try {
        auto j = json::parse(raw);
        if (!j.value("success", false)) return features;

        for (auto& f : j["features"]) {
            Feature feat;
            feat.tid     = f.value("tid", 0);
            feat.name    = f.value("name", "");
            feat.type    = f.value("type", "toggle");
            feat.enabled = f.value("default", 0) != 0;
            feat.value   = f.value("default", 0.0f);
            feat.min_val = f.value("min", 0.0f);
            feat.max_val = f.value("max", 100.0f);
            feat.step    = f.value("step", 1.0f);
            feat.unit    = f.value("unit", "");
            features[feat.tid] = feat;
        }
    } catch (...) {}

    return features;
}

int main() {
    // ── Cargar features públicas ──────────────────────────────────────────────
    auto features = LoadFeatures();

    std::cout << "Features cargadas: " << features.size() << "\n\n";

    for (auto& [tid, f] : features) {
        if (f.type == "toggle") {
            std::cout << "[Toggle] TID=" << tid << " | " << f.name
                      << " → " << (f.enabled ? "ON" : "OFF") << "\n";
        } else {
            std::cout << "[Slider] TID=" << tid << " | " << f.name
                      << " → " << f.value << f.unit
                      << " [" << f.min_val << "-" << f.max_val << "]\n";
        }
    }

    // ── Usar features por TID en tu código ───────────────────────────────────
    // Los TIDs los defines tú en el panel al crear cada feature

    // Ejemplo: TID 1 = Aimbot, TID 2 = FOV, TID 3 = ESP
    bool aimbotEnabled = false;
    float fovValue     = 90.0f;
    bool espEnabled    = false;

    if (features.count(1)) aimbotEnabled = features[1].enabled;
    if (features.count(2)) fovValue      = features[2].value;
    if (features.count(3)) espEnabled    = features[3].enabled;

    std::cout << "\nAimbot: " << (aimbotEnabled ? "ON" : "OFF") << "\n";
    std::cout << "FOV: " << fovValue << "\n";
    std::cout << "ESP: " << (espEnabled ? "ON" : "OFF") << "\n";

    // ── Cargar features de categoría específica con API key ───────────────────
    // Reemplaza con tu slug y API key del panel
    auto catFeatures = LoadCategoryFeatures("aimbot", "TU_API_KEY_AQUI");
    std::cout << "\nFeatures de categoria 'aimbot': " << catFeatures.size() << "\n";

    return 0;
}
