use actix_web::{HttpResponse, web};
use serde::Serialize;
use service::{AppService, users::public::PublicUserResponse};

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}/public",
    params(("username" = String, Path)),
    responses((status = 200, body = PublicUserResponse)),
    tag = "users"
)]
pub async fn user_public(
    service: web::Data<AppService>,
    username: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let result = service.users_public_by_username(&username).await?;
    ok_json(result)
}
