use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use session::Session;

#[utoipa::path(
    get,
    path = "/api/users/me/preferences",
    responses(
        (status = 200, description = "Get user preferences", body = ApiResponse<service::user::preferences::PreferencesResponse>),
        (status = 401, description = "Unauthorized"),
),
    tag = "User"
)]
pub async fn get_preferences(
    service: web::Data<AppService>,
    session: Session,
) -> Result<HttpResponse, ApiError> {
    let resp = service.user_get_preferences(&session).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    patch,
    path = "/api/users/me/preferences",
    request_body = service::user::preferences::PreferencesParams,
    responses(
        (status = 200, description = "Update user preferences", body = ApiResponse<service::user::preferences::PreferencesResponse>),
        (status = 401, description = "Unauthorized"),
),
    tag = "User"
)]
pub async fn update_preferences(
    service: web::Data<AppService>,
    session: Session,
    body: web::Json<service::user::preferences::PreferencesParams>,
) -> Result<HttpResponse, ApiError> {
    let resp = service
        .user_update_preferences(&session, body.into_inner())
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
