use actix_web::{HttpResponse, web};
use serde::Serialize;
use service::{
    AppService,
    user::appearance::{UpdateUserAppearanceConfig, UserAppearanceConfig},
};
use session::Session;

use crate::error::ApiError;

fn ok_json<T: Serialize>(data: T) -> Result<HttpResponse, ApiError> {
    Ok(HttpResponse::Ok().json(data))
}

#[utoipa::path(
    put,
    path = "/api/v1/user/config/appearance",
    request_body = UpdateUserAppearanceConfig,
    responses(
        (status = 200, body = UserAppearanceConfig)
    ),
    tag = "user"
)]
pub async fn update_appearance(
    service: web::Data<AppService>,
    session: Session,
    body: web::Json<UpdateUserAppearanceConfig>,
) -> Result<HttpResponse, ApiError> {
    let config = service
        .user_update_appearance_config(&session, body.into_inner())
        .await?;
    ok_json(config)
}
