use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use service::git::blame::BlameQuery;
use session::Session;

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/blame/{commit_oid}/{tail:.*}",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("commit_oid" = String, Path),
        ("tail" = String, Path, description = "File path within the repository"),
    ),
    responses(
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 200, body = Vec<service::git::blame::BlameHunkResponse>),
        (status = 404, description = "Not found", body = ApiResponse<ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_blame_file(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String, String)>,
    query: web::Query<BlameQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, commit_oid, file_path) = path.into_inner();
    let mut req = query.into_inner();
    req.commit_oid = commit_oid;
    req.path = file_path;
    let resp = service
        .git_blame_file(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
