/*
 * ============================================================
 *  Ejemplo 04 — Status de la app (Control de Status)
 *
 *  Endpoint público: GET /api/status/{slug}
 *  Endpoint anuncio: GET /api/status/{slug}/announcement
 *
 *  Desde el panel (Status) creas un status app con un slug.
 *  Puedes activarlo/desactivarlo y poner un anuncio.
 *
 *  Casos de uso:
 *    - Verificar si el cheat/app está online antes de cargar
 *    - Mostrar el anuncio del status (ej: "Actualizado para v1.2")
 *    - Bloquear el loader si el status está offline
 * ============================================================
 */
#include "SaoAuth.hpp"
#include <iostream>

const std::wstring PANEL_HOST = L"saoauth.com";

struct AppStatus {
    bool        found       = false;
    bool        online      = false;   // true = activo
    std::string name;
    std::string announcement;
};

AppStatus GetStatus(const std::string& slug) {
    std::wstring path = L"/api/status/" + std::wstring(slug.begin(), slug.end());
    std::string raw = SaoAuth::HttpGet(PANEL_HOST, path);

    AppStatus s;
    if (raw.empty()) return s;

    try {
        auto j = json::parse(raw);
        if (!j.value("success", false)) return s;

        auto& st = j["status"];
        s.found  = true;
        s.online = st.value("is_active", false);
        s.name   = st.value("name", "");

        // El anuncio viene en el mismo response o en endpoint separado
        if (j.contains("announcement"))
            s.announcement = j["announcement"].get<std::string>();
    } catch (...) {}

    return s;
}

std::string GetStatusAnnouncement(const std::string& slug) {
    std::wstring path = L"/api/status/" + std::wstring(slug.begin(), slug.end()) + L"/announcement";
    std::string raw = SaoAuth::HttpGet(PANEL_HOST, path);
    try {
        auto j = json::parse(raw);
        if (j.value("success", false))
            return j.value("announcement", "");
    } catch (...) {}
    return "";
}

int main() {
    // ── Verificar status antes de cargar ─────────────────────────────────────
    // Crea el status en el panel con slug "miapp"
    AppStatus status = GetStatus("miapp");

    if (!status.found) {
        MessageBoxA(nullptr,
            "No se pudo verificar el estado del servidor.\nRevisa tu conexión.",
            "Error de conexión", MB_ICONERROR);
        return 1;
    }

    if (!status.online) {
        // App offline — mostrar mensaje y salir
        std::string ann = GetStatusAnnouncement("miapp");
        std::string msg = status.name + " está actualmente OFFLINE.";
        if (!ann.empty()) msg += "\n\n" + ann;
        MessageBoxA(nullptr, msg.c_str(), "Servicio no disponible", MB_ICONWARNING);
        return 1;
    }

    // ── App online — mostrar anuncio si existe ────────────────────────────────
    std::string ann = GetStatusAnnouncement("miapp");
    if (!ann.empty()) {
        MessageBoxA(nullptr, ann.c_str(),
                    (status.name + " — Aviso").c_str(), MB_ICONINFORMATION);
    }

    std::cout << "[OK] " << status.name << " está ONLINE\n";

    // Continuar con la autenticación...
    return 0;
}
