//! `POST /api/audit/alarm` — security alarm (IP whitelist hit, brute-force
//! thresholds). Wire shape from CONSOLE_API.md §7.3:
//! `{ id, uuid, typ: int, info: stringified-JSON }`.

use crate::api::error::ApiError;
use crate::api::state::AppState;
use axum::extract::Extension;
use axum::http::StatusCode;
use axum::Json;
use serde::Deserialize;
use std::sync::Arc;

#[derive(Debug, Deserialize)]
pub struct AlarmAuditBody {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub uuid: String,
    #[serde(default)]
    pub typ: i64,
    #[serde(default)]
    pub info: String,
}

pub async fn alarm(
    Extension(state): Extension<Arc<AppState>>,
    Json(body): Json<AlarmAuditBody>,
) -> Result<StatusCode, ApiError> {
    if body.id.is_empty() {
        return Err(ApiError::BadRequest("id required".into()));
    }
    state
        .db
        .audit_alarm_insert(&body.id, body.typ, &body.info)
        .await
        .map_err(|e| ApiError::Internal(e.to_string()))?;
    Ok(StatusCode::OK)
}
