//! 告警通知：POST JSON 到 webhook（Telegram / Uptime Kuma / 自定义）。

use std::sync::OnceLock;

static CLIENT: OnceLock<reqwest::Client> = OnceLock::new();

fn client() -> &'static reqwest::Client {
    CLIENT.get_or_init(|| {
        reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(5))
            .build()
            .unwrap_or_else(|_| reqwest::Client::new())
    })
}

pub async fn notify(webhook_url: &str, message: &str) {
    if webhook_url.is_empty() {
        tracing::warn!(message, "【告警】");
        return;
    }
    let body = serde_json::json!({ "text": message, "message": message });
    match client().post(webhook_url).json(&body).send().await {
        Ok(resp) => tracing::info!(status = %resp.status(), "告警 webhook 已推送"),
        Err(e) => tracing::warn!(%e, "告警 webhook 推送失败"),
    }
}
