//! Server config API handlers.

use axum::{extract::State, Json};
use chattyness_db::{
    models::{ServerConfig, UpdateServerConfigRequest},
    queries::owner as queries,
};
use chattyness_error::AppError;
use sqlx::PgPool;

/// Get server config.
pub async fn get_config(
    State(pool): State<PgPool>,
) -> Result<Json<ServerConfig>, AppError> {
    let config = queries::get_server_config(&pool).await?;
    Ok(Json(config))
}

/// Update server config.
pub async fn update_config(
    State(pool): State<PgPool>,
    Json(req): Json<UpdateServerConfigRequest>,
) -> Result<Json<ServerConfig>, AppError> {
    req.validate()?;
    let config = queries::update_server_config(&pool, &req).await?;
    Ok(Json(config))
}
