use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use service::git::archive::{
    ArchiveCachedResponse, ArchiveInvalidateAllResponse, ArchiveInvalidateResponse,
    ArchiveListResponse, ArchiveQuery, ArchiveResponse, ArchiveSummaryResponse,
};
use session::Session;

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/archive",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("commit_oid" = String, Query),
        ("format" = String, Query),
        ("prefix" = Option<String>, Query),
        ("max_depth" = Option<usize>, Query),
        ("path_filter" = Option<String>, Query),
    ),
    responses(
        (status = 200, description = "Get archive", body = ApiResponse<ArchiveResponse>),
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_archive(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<ArchiveQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_archive(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/archive/list",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("commit_oid" = String, Query),
        ("format" = String, Query),
        ("prefix" = Option<String>, Query),
        ("max_depth" = Option<usize>, Query),
        ("path_filter" = Option<String>, Query),
    ),
    responses(
        (status = 200, description = "List archive entries", body = ApiResponse<ArchiveListResponse>),
        (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_archive_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<ArchiveQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_archive_list(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/archive/summary",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("commit_oid" = String, Query),
        ("format" = String, Query),
        ("prefix" = Option<String>, Query),
        ("max_depth" = Option<usize>, Query),
        ("path_filter" = Option<String>, Query),
    ),
    responses(
        (status = 200, description = "Get archive summary", body = ApiResponse<ArchiveSummaryResponse>),
        (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_archive_summary(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<ArchiveQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_archive_summary(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/archive/cached",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("commit_oid" = String, Query),
        ("format" = String, Query),
        ("prefix" = Option<String>, Query),
        ("max_depth" = Option<usize>, Query),
        ("path_filter" = Option<String>, Query),
    ),
    responses(
        (status = 200, description = "Check if archive is cached", body = ApiResponse<ArchiveCachedResponse>),
        (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_archive_cached(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<ArchiveQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_archive_cached(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/archive/invalidate",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("commit_oid" = String, Query),
        ("format" = String, Query),
        ("prefix" = Option<String>, Query),
        ("max_depth" = Option<usize>, Query),
        ("path_filter" = Option<String>, Query),
    ),
    responses(
        (status = 200, description = "Invalidate archive cache", body = ApiResponse<ArchiveInvalidateResponse>),
        (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_archive_invalidate(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<ArchiveQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_archive_invalidate(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/archive/invalidate/{commit_oid}",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("commit_oid" = String, Path),
    ),
    responses(
        (status = 200, description = "Invalidate all archive caches for commit", body = ApiResponse<ArchiveInvalidateAllResponse>),
        (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_archive_invalidate_all(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, commit_oid) = path.into_inner();
    let resp = service
        .git_archive_invalidate_all(namespace, repo_name, commit_oid, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
