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

#[utoipa::path(
    get,
    path = "/api/issue/{project}/labels",
    params(("project" = String, Path)),
    responses(
        (status = 200, description = "List labels", body = ApiResponse<Vec<service::issue::LabelResponse>>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
    ),
    tag = "Issues"
)]
pub async fn label_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let project = path.into_inner();
    let resp = service.label_list(project, &session).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/issue/{project}/labels",
    params(("project" = String, Path)),
    request_body = service::issue::CreateLabelRequest,
    responses(
        (status = 200, description = "Create label", body = ApiResponse<service::issue::LabelResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "Issues"
)]
pub async fn label_create(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
    body: web::Json<service::issue::CreateLabelRequest>,
) -> Result<HttpResponse, ApiError> {
    let project = path.into_inner();
    let resp = service
        .label_create(project, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/issue/{project}/labels/{label_id}",
    params(
        ("project" = String, Path),
        ("label_id" = i64, Path),
    ),
    responses(
        (status = 200, description = "Delete label"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "Issues"
)]
pub async fn label_delete(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (project, label_id) = path.into_inner();
    service.label_delete(project, label_id, &session).await?;
    Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}
