use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use service::git::commit::{
    CommitAmendRequest, CommitAncestorsQuery, CommitCherryPickAbortRequest,
    CommitCherryPickRequest, CommitCreateRequest, CommitCreateResponse, CommitDescendantsQuery,
    CommitGetQuery, CommitGraphReactResponse, CommitLogQuery, CommitLogResponse,
    CommitResolveQuery, CommitRevertAbortRequest, CommitRevertRequest, CommitWalkQuery,
};
use session::Session;

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit metadata", body = ApiResponse<service::git::commit::CommitMetaResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_commit_get(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_get(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/exists",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Check if commit exists", body = ApiResponse<service::git::commit::CommitExistsResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_exists(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_exists(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/is-commit",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Check if object is a commit", body = ApiResponse<service::git::commit::CommitIsCommitResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_is_commit(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_is_commit(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/message",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit message", body = ApiResponse<service::git::commit::CommitMessageResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_message(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_message(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/summary",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit summary", body = ApiResponse<service::git::commit::CommitSummaryResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_summary(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_summary(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/short-id",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit short ID", body = ApiResponse<service::git::commit::CommitShortIdResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_short_id(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_short_id(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/author",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit author", body = ApiResponse<service::git::commit::CommitAuthorResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_author(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_author(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/tree-id",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit tree ID", body = ApiResponse<service::git::commit::CommitTreeIdResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_tree_id(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_tree_id(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/parent-count",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit parent count", body = ApiResponse<service::git::commit::CommitParentCountResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_parent_count(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_parent_count(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/parent-ids",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit parent IDs", body = ApiResponse<service::git::commit::CommitParentIdsResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_parent_ids(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_parent_ids(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/parent/{index}",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("index" = usize, Path),
    ),
    responses(
        (status = 200, description = "Get commit parent", body = ApiResponse<service::git::commit::CommitMetaResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_parent(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String, usize)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid, index) = path.into_inner();
    let resp = service
        .git_commit_parent(namespace, repo_name, oid, index, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/first-parent",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit first parent", body = ApiResponse<Option<service::git::commit::CommitMetaResponse>>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_first_parent(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_first_parent(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/is-merge",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Check if commit is a merge", body = ApiResponse<service::git::commit::CommitIsMergeResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_is_merge(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_is_merge(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("rev" = Option<String>, Query),
        ("per_page" = Option<usize>, Query),
        ("page" = Option<usize>, Query),
    ),
    responses(
        (status = 200, description = "Get commit log (paginated)", body = ApiResponse<CommitLogResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_log(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<CommitLogQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_commit_log(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/count",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit count", body = ApiResponse<service::git::commit::CommitCountResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_count(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<CommitCountQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_commit_count(
            namespace,
            repo_name,
            query.from.clone(),
            query.to.clone(),
            &session,
        )
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/refs",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit refs", body = ApiResponse<Vec<service::git::commit::CommitRefInfoResponse>>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_refs(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_refs(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/branches",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit branches", body = ApiResponse<service::git::commit::CommitBranchesResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_branches(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_commit_branches(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/tags",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit tags", body = ApiResponse<service::git::commit::CommitTagsResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_tags(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_commit_tags(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/is-tip",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Check if commit is a tip", body = ApiResponse<service::git::commit::CommitIsTipResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_is_tip(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_is_tip(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/ref-count",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit ref count", body = ApiResponse<service::git::commit::CommitRefCountResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_ref_count(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_ref_count(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/reflog",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit reflog", body = ApiResponse<Vec<service::git::commit::CommitReflogEntryResponse>>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_reflog(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<CommitGetQuery>,
    refname: web::Query<CommitReflogQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_commit_reflog(
            namespace,
            repo_name,
            query.into_inner(),
            refname.refname.clone(),
            &session,
        )
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/graph",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit graph", body = ApiResponse<service::git::commit::CommitGraphResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_graph(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<CommitWalkQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_commit_graph(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

/// Returns commit graph data enriched with full commit metadata (author, timestamp,
/// parents, lane_index) for use with @gitgraph/react on the frontend.
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/graph-react",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit graph for gitgraph-react", body = ApiResponse<CommitGraphReactResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_commit_graph_react(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<CommitWalkQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_commit_graph_react(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/walk",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Walk commits", body = ApiResponse<Vec<service::git::commit::CommitMetaResponse>>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_walk(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<CommitWalkQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_commit_walk(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/ancestors",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit ancestors", body = ApiResponse<Vec<service::git::commit::CommitMetaResponse>>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_ancestors(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitAncestorsQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_ancestors(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/descendants",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Get commit descendants", body = ApiResponse<Vec<service::git::commit::CommitMetaResponse>>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_descendants(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<CommitDescendantsQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_commit_descendants(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/commits/resolve",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    responses(
        (status = 200, description = "Resolve revision to commit", body = ApiResponse<String>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_resolve_rev(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<CommitResolveQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_commit_resolve_rev(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo}/git/commits",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    request_body = CommitCreateRequest,
    responses(
        (status = 200, description = "Create commit", body = ApiResponse<CommitCreateResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_create(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    body: web::Json<CommitCreateRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_commit_create(namespace, repo_name, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    patch,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/amend",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    request_body = CommitAmendRequest,
    responses(
        (status = 200, description = "Amend commit", body = ApiResponse<service::git::commit::CommitMetaResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_amend(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    body: web::Json<CommitAmendRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, _oid) = path.into_inner();
    let resp = service
        .git_commit_amend(namespace, repo_name, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/cherry-pick",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    request_body = CommitCherryPickRequest,
    responses(
        (status = 200, description = "Cherry-pick commit", body = ApiResponse<service::git::commit::CommitMetaResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_cherry_pick(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    body: web::Json<CommitCherryPickRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, _oid) = path.into_inner();
    let resp = service
        .git_commit_cherry_pick(namespace, repo_name, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/cherry-pick/abort",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    request_body = CommitCherryPickAbortRequest,
    responses(
        (status = 200, description = "Abort cherry-pick", body = ApiResponse<bool>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_cherry_pick_abort(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    body: web::Json<CommitCherryPickAbortRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, _oid) = path.into_inner();
    service
        .git_commit_cherry_pick_abort(namespace, repo_name, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(true).to_response())
}

#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/revert",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    request_body = CommitRevertRequest,
    responses(
        (status = 200, description = "Revert commit", body = ApiResponse<service::git::commit::CommitMetaResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_revert(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    body: web::Json<CommitRevertRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, _oid) = path.into_inner();
    let resp = service
        .git_commit_revert(namespace, repo_name, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo}/git/commits/{oid}/revert/abort",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
    ),
    request_body = CommitRevertAbortRequest,
    responses(
        (status = 200, description = "Abort revert", body = ApiResponse<bool>),
        (status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"

)]
pub async fn git_commit_revert_abort(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    body: web::Json<CommitRevertAbortRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, _oid) = path.into_inner();
    service
        .git_commit_revert_abort(namespace, repo_name, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(true).to_response())
}

// Query helpers
#[derive(serde::Deserialize, utoipa::IntoParams)]
pub struct CommitCountQuery {
    pub from: Option<String>,
    pub to: Option<String>,
}

#[derive(serde::Deserialize, utoipa::IntoParams)]
pub struct CommitReflogQuery {
    pub refname: Option<String>,
}
