use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use service::git::diff::{
    DiffCommitQuery, DiffPatchIdResponse, DiffQuery, DiffResultResponse, DiffStatsResponse,
    SideBySideDiffQuery, SideBySideDiffResponse,
};
use session::Session;

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/diff",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("old_tree" = String, Query, description = "Old tree OID (commit or tree SHA)"),
        ("new_tree" = String, Query, description = "New tree OID (commit or tree SHA)"),
    ),
    responses(
        (status = 200, description = "Tree to tree diff", body = ApiResponse<DiffResultResponse>),
        (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_diff_tree_to_tree(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<DiffQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_diff_tree_to_tree(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/diff/commit/{commit}",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("commit" = String, Path, description = "Commit identifier"),
    ),
    responses(
        (status = 200, description = "Commit to workdir diff", body = ApiResponse<DiffResultResponse>),
        (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_diff_commit_to_workdir(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<DiffCommitQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, commit) = path.into_inner();
    let mut req = query.into_inner();
    req.commit = commit;
    let resp = service
        .git_diff_commit_to_workdir(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/diff/commit/{commit}/index",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("commit" = String, Path, description = "Commit identifier"),
    ),
    responses(
        (status = 200, description = "Commit to index diff", body = ApiResponse<DiffResultResponse>),
        (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_diff_commit_to_index(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<DiffCommitQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, commit) = path.into_inner();
    let mut req = query.into_inner();
    req.commit = commit;
    let resp = service
        .git_diff_commit_to_index(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/diff/workdir",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    responses(
        (status = 200, description = "Workdir to index diff", body = ApiResponse<DiffResultResponse>),
        (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_diff_workdir_to_index(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<DiffQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_diff_workdir_to_index(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/diff/index",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    responses(
        (status = 200, description = "Index to tree diff", body = ApiResponse<DiffResultResponse>),
        (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_diff_index_to_tree(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<DiffQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_diff_index_to_tree(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/diff/stats",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    responses(
        (status = 200, description = "Diff statistics", body = ApiResponse<DiffStatsResponse>),
        (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_diff_stats(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<DiffQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_diff_stats(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/diff/patch-id",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    responses(
        (status = 200, description = "Patch ID", body = ApiResponse<DiffPatchIdResponse>),
        (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_diff_patch_id(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<DiffQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_diff_patch_id(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/diff/side-by-side",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    responses(
        (status = 200, description = "Side-by-side diff", body = ApiResponse<SideBySideDiffResponse>),
        (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_diff_side_by_side(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<SideBySideDiffQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_diff_side_by_side(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
