use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use service::git::webhook::{
    CreateWebhookParams, UpdateWebhookParams, WebhookListResponse, WebhookResponse,
};
use session::Session;

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/webhooks",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
    ),
    responses(
        (status = 200, description = "List webhooks", body = ApiResponse<WebhookListResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
    ),
    tag = "Git"
)]
pub async fn git_webhook_list(
    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_webhook_list(namespace, repo_name, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

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

#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo}/git/webhooks/{webhook_id}",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("webhook_id" = i64, Path, description = "Webhook ID"),
    ),
    responses(
        (status = 200, description = "Get webhook", body = ApiResponse<WebhookResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
    ),
    tag = "Git"
)]
pub async fn git_webhook_get(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, webhook_id) = path.into_inner();
    let resp = service
        .git_webhook_get(namespace, repo_name, webhook_id, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    patch,
    path = "/api/repos/{namespace}/{repo}/git/webhooks/{webhook_id}",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("webhook_id" = i64, Path, description = "Webhook ID"),
    ),
    request_body = UpdateWebhookParams,
    responses(
        (status = 200, description = "Update webhook", body = ApiResponse<WebhookResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "Git"
)]
pub async fn git_webhook_update(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64)>,
    body: web::Json<UpdateWebhookParams>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, webhook_id) = path.into_inner();
    let resp = service
        .git_webhook_update(
            namespace,
            repo_name,
            webhook_id,
            body.into_inner(),
            &session,
        )
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/repos/{namespace}/{repo}/git/webhooks/{webhook_id}",
    params(
        ("namespace" = String, Path, description = "Repository namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("webhook_id" = i64, Path, description = "Webhook ID"),
    ),
    responses(
        (status = 200, description = "Delete webhook"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "Git"
)]
pub async fn git_webhook_delete(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo_name, webhook_id) = path.into_inner();
    service
        .git_webhook_delete(namespace, repo_name, webhook_id, &session)
        .await?;
    Ok(ApiResponse::ok(true).to_response())
}
