use actix_web::{HttpResponse, web};
use serde::Serialize;
use service::{AppService, users::summary::UserSummaryResponse};

use crate::error::ApiError;

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

#[utoipa::path(
    get,
    path = "/api/v1/users/{username}/summary",
    params(("username" = String, Path)),
    responses((status = 200, body = UserSummaryResponse)),
    tag = "users"
)]
pub async fn user_summary(
    service: web::Data<AppService>,
    username: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let result = service.users_summary_by_username(&username).await?;
    ok_json(result)
}
