use actix_web::{HttpRequest, HttpResponse, web};
use channel::ChannelBus;
use channel::http::{WsHandler, WsInMessage};

use super::rest::{channel_err, extract_user, ok_json};
use crate::error::ApiError;

#[utoipa::path(
    get,
    path = "/api/v1/ws/users/summary/{username}",
    responses((status = 200, description = "User summary")),
    tag = "channel",
)]
pub async fn user_summary(
    req: HttpRequest,
    username: web::Path<String>,
    bus: web::Data<ChannelBus>,
) -> Result<HttpResponse, ApiError> {
    let _user_id = extract_user(&req)?;
    let msg = WsInMessage::UserSummary {
        username: username.into_inner(),
    };
    let result = WsHandler::handle(&bus, _user_id, msg)
        .await
        .map_err(channel_err)?;
    Ok(ok_json(result))
}
