use actix_web::{HttpResponse, web};
use serde::Serialize;
use service::{
    AppService,
    user::chpc::{ContributionHeatmapQuery, ContributionHeatmapResponse},
};

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}/contribution-heatmap",
    params(("username" = String, Path), ContributionHeatmapQuery),
    responses((status = 200, body = ContributionHeatmapResponse)),
    tag = "users"
)]
pub async fn user_chpc(
    service: web::Data<AppService>,
    username: web::Path<String>,
    query: web::Query<ContributionHeatmapQuery>,
) -> Result<HttpResponse, ApiError> {
    let result = service
        .users_chpc_by_username(&username, query.into_inner())
        .await?;
    ok_json(result)
}
