use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use service::git::refs::{
    RefCreateRequest, RefDeleteResponse, RefExistsResponse, RefInfoResponse, RefListQuery,
    RefRenameQuery, RefTargetResponse, RefUpdateRequest, RefUpdateResponse,
};
use session::Session;

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/refs",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    responses(
        (status = 200, description = "List of refs", body = ApiResponse<Vec<RefInfoResponse>>),
        (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_ref_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<RefListQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_ref_list(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/refs/{name}",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("name" = String, Path, description = "Ref name"),
    ),
    responses(
        (status = 200, description = "Ref info", body = ApiResponse<RefInfoResponse>),
        (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_ref_get(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, name) = path.into_inner();
    let resp = service
        .git_ref_get(namespace, repo_name, name, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo}/git/refs",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    request_body = RefCreateRequest,
    responses(
        (status = 200, description = "Ref created", body = ApiResponse<RefUpdateResponse>),
        (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_ref_create(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    body: web::Json<RefCreateRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_ref_create(namespace, repo_name, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/repos/{namespace}/{repo}/git/refs/{name}",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("name" = String, Path, description = "Ref name"),
    ),
    responses(
        (status = 200, description = "Ref deleted", body = ApiResponse<RefDeleteResponse>),
        (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_ref_delete(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, name) = path.into_inner();
    let resp = service
        .git_ref_delete(namespace, repo_name, name, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    patch,
    path = "/api/repos/{namespace}/{repo}/git/refs/rename",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    responses(
        (status = 200, description = "Ref renamed", body = ApiResponse<RefInfoResponse>),
        (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_ref_rename(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<RefRenameQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_ref_rename(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    put,
    path = "/api/repos/{namespace}/{repo}/git/refs",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    request_body = RefUpdateRequest,
    responses(
        (status = 200, description = "Ref updated", body = ApiResponse<RefUpdateResponse>),
        (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_ref_update(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    body: web::Json<RefUpdateRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_ref_update(namespace, repo_name, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/refs/{name}/exists",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("name" = String, Path, description = "Ref name"),
    ),
    responses(
        (status = 200, description = "Ref exists check", body = ApiResponse<RefExistsResponse>),
        (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_ref_exists(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, name) = path.into_inner();
    let resp = service
        .git_ref_exists(namespace, repo_name, name, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/refs/{name}/target",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("name" = String, Path, description = "Ref name"),
    ),
    responses(
        (status = 200, description = "Ref target", body = ApiResponse<RefTargetResponse>),
        (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_ref_target(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, String)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, name) = path.into_inner();
    let resp = service
        .git_ref_target(namespace, repo_name, name, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
