//! `POST /api/audit/file` — file transfer log entry (CONSOLE_API.md §7.2).
//! `info` arrives as a stringified JSON object; we store it verbatim.

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 FileAuditBody {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub uuid: String,
    #[serde(default)]
    pub peer_id: String,
    #[serde(default, rename = "type")]
    pub direction: i64,
    #[serde(default)]
    pub path: String,
    #[serde(default)]
    pub is_file: bool,
    #[serde(default)]
    pub info: String,
}

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