use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use service::git::init::GitInitRequest;
use session::Session;

#[utoipa::path(
    post,
    path = "/api/git/init",
    request_body = GitInitRequest,
    responses(
        (status = 200, description = "Bare repository initialized", body = ApiResponse<service::git::init::GitInitResponse>),
        (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_init_bare(
    service: web::Data<AppService>,
    body: web::Json<GitInitRequest>,
) -> Result<HttpResponse, ApiError> {
    let resp = service.git_init_bare(body.into_inner()).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/git/open/{path}",
    params(
        ("path" = String, Path, description = "Repository path"),
    ),
    responses(
        (status = 200, description = "Open repository", body = ApiResponse<service::git::init::GitInitResponse>),
        (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_open(
    service: web::Data<AppService>,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let resp = service.git_open(path.into_inner()).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/git/open/{path}/workdir",
    params(
        ("path" = String, Path, description = "Repository path"),
    ),
    responses(
        (status = 200, description = "Open repository working directory", body = ApiResponse<service::git::init::GitInitResponse>),
        (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_open_workdir(
    service: web::Data<AppService>,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let resp = service.git_open_workdir(path.into_inner()).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/git/is-repo/{path}",
    params(
        ("path" = String, Path, description = "Repository path"),
    ),
    responses(
        (status = 200, description = "Check if path is a repository", body = ApiResponse<bool>),
        (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_is_repo(
    service: web::Data<AppService>,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let resp = service.git_is_repo(path.into_inner()).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

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