use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use service::git::blob::{
    BlobContentResponse, BlobCreateRequest, BlobCreateResponse, BlobExistsResponse, BlobGetQuery,
    BlobInfoResponse, BlobIsBinaryResponse, BlobSizeResponse, GitReadmeQuery, GitReadmeResponse,
};
use session::Session;

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/readme",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("ref" = Option<String>, Query, description = "Git reference (branch, tag, commit). Defaults to HEAD."),
    ),
    responses(
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 200, description = "Get README content", body = ApiResponse<GitReadmeResponse>),
    ),
    tag = "Git"
)]
pub async fn git_readme(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<GitReadmeQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_readme(namespace, repo_name, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

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

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

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

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

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

#[utoipa::path(
    post,
    path = "/api/repos/{namespace}/{repo}/git/blob",
    request_body = BlobCreateRequest,
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    responses(
        (status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
        (status = 200, description = "Create blob", body = ApiResponse<BlobCreateResponse>),
        (status = 404, description = "Not found", body = ApiResponse<ApiError>),
    ),
    tag = "Git"
)]
pub async fn git_blob_create(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    body: web::Json<BlobCreateRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name) = path.into_inner();
    let resp = service
        .git_blob_create(namespace, repo_name, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
