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

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

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

#[utoipa::path(
    get,
    path = "/api/projects/{project_name}/labels/{label_id}",
    params(
        ("project_name" = String, Path),
        ("label_id" = i64, Path),
    ),
    responses(
        (status = 200, description = "Get project label", body = ApiResponse<service::project::labels::LabelResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_get_label(
    service: web::Data<AppService>,
    _session: Session,
    path: web::Path<(String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (_project_name, label_id) = path.into_inner();
    let resp = service.project_get_label(label_id).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

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

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