use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use service::git::tree::{
    TreeDiffQuery, TreeDiffStatsResponse, TreeEntryByCommitPathQuery, TreeEntryByPathQuery,
    TreeEntryCountResponse, TreeEntryQuery, TreeEntryResponse, TreeExistsResponse, TreeGetQuery,
    TreeInfoResponse, TreeIsEmptyResponse,
};
use session::Session;

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/tree/{oid}",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("oid" = String, Path, description = "Tree object ID"),
    ),
    responses(
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 200, description = "Get tree info", body = ApiResponse<TreeInfoResponse>),
        (status = 404, description = "Not found", body = ApiResponse<ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_tree_get(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<TreeGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_tree_get(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/tree/{oid}/exists",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("oid" = String, Path, description = "Tree object ID"),
    ),
    responses(
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 200, description = "Check if tree exists", body = ApiResponse<TreeExistsResponse>),
        (status = 404, description = "Not found", body = ApiResponse<ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_tree_exists(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<TreeGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_tree_exists(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/tree/{oid}/list",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("oid" = String, Path, description = "Tree object ID"),
    ),
    responses(
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 200, description = "List tree entries", body = ApiResponse<Vec<TreeEntryResponse>>),
        (status = 404, description = "Not found", body = ApiResponse<ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_tree_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<TreeGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_tree_list(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/tree/{oid}/entry/{index}",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("oid" = String, Path, description = "Tree object ID"),
        ("index" = usize, Path, description = "Entry index"),
    ),
    responses(
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 200, description = "Get tree entry by index", body = ApiResponse<TreeEntryResponse>),
        (status = 404, description = "Not found", body = ApiResponse<ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_tree_entry(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String, usize)>,
    query: web::Query<TreeEntryQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid, index) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    req.index = index;
    let resp = service
        .git_tree_entry(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/tree/{oid}/entry-by-path",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("oid" = String, Path, description = "Tree object ID"),
    ),
    responses(
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 200, description = "Get tree entry by path", body = ApiResponse<TreeEntryResponse>),
        (status = 404, description = "Not found", body = ApiResponse<ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_tree_entry_by_path(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<TreeEntryByPathQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_tree_entry_by_path(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/tree/{commit}/commit-entry-by-path",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("commit" = String, Path, description = "Commit OID"),
    ),
    responses(
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 200, description = "Get tree entry by commit path", body = ApiResponse<TreeEntryResponse>),
        (status = 404, description = "Not found", body = ApiResponse<ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_tree_entry_by_commit_path(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<TreeEntryByCommitPathQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, commit) = path.into_inner();
    let mut req = query.into_inner();
    req.commit = commit;
    let resp = service
        .git_tree_entry_by_commit_path(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/tree/{oid}/entry-count",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("oid" = String, Path, description = "Tree object ID"),
    ),
    responses(
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 200, description = "Get tree entry count", body = ApiResponse<TreeEntryCountResponse>),
        (status = 404, description = "Not found", body = ApiResponse<ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_tree_entry_count(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<TreeGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_tree_entry_count(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/tree/{oid}/is-empty",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("oid" = String, Path, description = "Tree object ID"),
    ),
    responses(
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 200, description = "Check if tree is empty", body = ApiResponse<TreeIsEmptyResponse>),
        (status = 404, description = "Not found", body = ApiResponse<ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_tree_is_empty(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
    query: web::Query<TreeGetQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, oid) = path.into_inner();
    let mut req = query.into_inner();
    req.oid = oid;
    let resp = service
        .git_tree_is_empty(namespace, repo_name, req, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/tree/diff-stats",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    responses(
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 200, description = "Get tree diff stats", body = ApiResponse<TreeDiffStatsResponse>),
        (status = 404, description = "Not found", body = ApiResponse<ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_tree_diffstats(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<TreeDiffQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_tree_diffstats(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
