/*
 * ============================================================
 *  Ejemplo 03 — Notices (anuncios del panel)
 *
 *  Endpoint público: GET /api/notices/{slug}
 *  No requiere autenticación — cualquier cliente puede leerlo.
 *
 *  Desde el panel (Anuncios) creas un notice con un slug,
 *  lo activas, y tu C++ lo lee en tiempo real.
 *
 *  Casos de uso:
 *    - Mostrar un aviso de mantenimiento
 *    - Anunciar una nueva versión
 *    - Mensaje de bienvenida dinámico
 *    - Alerta de seguridad
 * ============================================================
 */
#include "SaoAuth.hpp"
#include <iostream>

// Host de tu panel
const std::wstring PANEL_HOST = L"saoauth.com";

struct Notice {
    bool        found   = false;
    bool        active  = false;
    std::string title;
    std::string message;
    std::string type;   // "info" | "warning" | "danger" | "success"
};

Notice GetNotice(const std::string& slug) {
    std::wstring path = L"/api/notices/" + std::wstring(slug.begin(), slug.end());
    std::string raw = SaoAuth::HttpGet(PANEL_HOST, path);

    Notice n;
    if (raw.empty()) return n;

    try {
        auto j = json::parse(raw);
        if (!j.value("success", false)) return n;

        auto& notice = j["notice"];
        n.found   = true;
        n.active  = notice.value("is_active", false);
        n.title   = notice.value("title",   "");
        n.message = notice.value("message", "");
        n.type    = notice.value("type",    "info");
    } catch (...) {}

    return n;
}

void ShowNotice(const Notice& n) {
    if (!n.found || !n.active) return;

    UINT icon = MB_ICONINFORMATION;
    if (n.type == "warning") icon = MB_ICONWARNING;
    if (n.type == "danger")  icon = MB_ICONERROR;

    std::string text = n.message;
    MessageBoxA(nullptr, text.c_str(), n.title.c_str(), icon);
}

int main() {
    // ── Leer el notice con slug "bienvenida" ──────────────────────────────────
    // Créalo en el panel: Anuncios → Crear → slug: "bienvenida"
    Notice welcome = GetNotice("bienvenida");
    ShowNotice(welcome);

    // ── Leer notice de mantenimiento ──────────────────────────────────────────
    Notice maint = GetNotice("mantenimiento");
    if (maint.found && maint.active) {
        MessageBoxA(nullptr, maint.message.c_str(),
                    "Mantenimiento programado", MB_ICONWARNING);
        // Podrías salir del programa si hay mantenimiento
        // return 0;
    }

    // ── Leer notice de actualización ─────────────────────────────────────────
    Notice update = GetNotice("actualizacion");
    if (update.found && update.active) {
        int resp = MessageBoxA(nullptr,
            (update.message + "\n\n¿Descargar ahora?").c_str(),
            update.title.c_str(),
            MB_YESNO | MB_ICONINFORMATION);
        if (resp == IDYES) {
            // Abrir link de descarga
            ShellExecuteA(nullptr, "open", "https://tu-sitio.com/download", nullptr, nullptr, SW_SHOW);
        }
    }

    std::cout << "Notices cargados correctamente\n";
    return 0;
}
