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