use actix_web::{HttpResponse, web};
use serde::Serialize;
use service::{
    AppService,
    user::accessibility::{
        UpdateUserAccessibilityConfig, UserAccessibilityConfig,
    },
};
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/accessibility",
    request_body = UpdateUserAccessibilityConfig,
    responses(
        (status = 200, body = UserAccessibilityConfig)
    ),
    tag = "user"
)]
pub async fn update_accessibility(
    service: web::Data<AppService>,
    session: Session,
    body: web::Json<UpdateUserAccessibilityConfig>,
) -> Result<HttpResponse, ApiError> {
    let config = service
        .user_update_accessibility_config(&session, body.into_inner())
        .await?;
    ok_json(config)
}
