//! `PUT /api/audit` — operator end-of-session note. Sent from the Flutter
//! `_showConnEndAuditDialogCloseCanceled` flow at
//! [flutter/lib/common/widgets/dialog.dart:1656](file:///Users/sn0/Desktop/rustdesk/flutter/lib/common/widgets/dialog.dart#L1656).
//! Bearer-authenticated.

use crate::api::error::ApiError;
use crate::api::middleware::AuthedUser;
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 NoteBody {
    pub guid: String,
    #[serde(default)]
    pub note: String,
}

pub async fn note(
    Extension(state): Extension<Arc<AppState>>,
    _user: AuthedUser,
    Json(body): Json<NoteBody>,
) -> Result<StatusCode, ApiError> {
    if body.guid.is_empty() {
        return Err(ApiError::BadRequest("guid required".into()));
    }
    let updated = state
        .db
        .audit_conn_update_note(&body.guid, &body.note)
        .await
        .map_err(|e| ApiError::Internal(e.to_string()))?;
    if !updated {
        return Err(ApiError::NotFound);
    }
    Ok(StatusCode::OK)
}
