use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{AppService, Pagination};
use session::Session;

use crate::error::ApiError;

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

#[derive(Deserialize, utoipa::IntoParams)]
pub struct WkRepoPath {
    pub wk: String,
    pub repo: String,
}
#[utoipa::path(
    get, path = "/api/v1/workspace/{wk}/repos/{repo}/git/contributors",
    params(WkRepoPath, Pagination),
    responses((status = 200, description = "Contributor list", body = Vec<service::git::contributor::ContributorDto>)),
    security(("session" = []))
)]
pub async fn list_contributors(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<WkRepoPath>,
    pagination: web::Query<Pagination>,
) -> Result<HttpResponse, ApiError> {
    let WkRepoPath { wk, repo } = path.into_inner();
    let data = service
        .git_repo_contributors(&session, &wk, &repo, pagination.into_inner())
        .await?;
    ok_json(data)
}
