use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService,
    issues::{
        label::{AddIssueLabel, CreateLabel, UpdateLabel},
        types::LabelResponse,
    },
};
use session::Session;
use uuid::Uuid;

use crate::error::ApiError;

fn ok_json<T: Serialize>(data: T) -> Result<HttpResponse, ApiError> {
    Ok(HttpResponse::Ok().json(data))
}

fn ok() -> Result<HttpResponse, ApiError> {
    Ok(HttpResponse::Ok().finish())
}

#[derive(Deserialize, utoipa::IntoParams)]
pub struct LabelIdPath {
    pub wk: String,
    pub label_id: Uuid,
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/labels",
    params(
        ("wk" = String, Path, description = "Workspace name"),
    ),
    request_body = CreateLabel,
    responses(
        (status = 200, body = LabelResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn create_label(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
    params: web::Json<CreateLabel>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service
        .label_create(&session, &wk, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/labels",
    params(
        ("wk" = String, Path, description = "Workspace name"),
    ),
    responses(
        (status = 200, body = Vec<LabelResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn list_labels(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service.label_list(&session, &wk).await?;
    ok_json(data)
}
#[utoipa::path(
    put,
    path = "/api/v1/workspace/{wk}/labels/{label_id}",
    params(LabelIdPath),
    request_body = UpdateLabel,
    responses(
        (status = 200, body = LabelResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
        (status = 404, description = "Label not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn update_label(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<LabelIdPath>,
    params: web::Json<UpdateLabel>,
) -> Result<HttpResponse, ApiError> {
    let LabelIdPath { wk, label_id } = path.into_inner();
    let data = service
        .label_update(&session, &wk, label_id, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/labels/{label_id}",
    params(LabelIdPath),
    responses(
        (status = 200, description = "Label deleted"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
        (status = 404, description = "Label not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn delete_label(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<LabelIdPath>,
) -> Result<HttpResponse, ApiError> {
    let LabelIdPath { wk, label_id } = path.into_inner();
    service.label_delete(&session, &wk, label_id).await?;
    ok()
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/issues/{number}/labels",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    request_body = AddIssueLabel,
    responses(
        (status = 200, body = Vec<LabelResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue or label not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn add_issue_label(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
    params: web::Json<AddIssueLabel>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    let data = service
        .issue_add_label(&session, &wk, number, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/issues/{number}/labels",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    responses(
        (status = 200, body = Vec<LabelResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue or label not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn remove_issue_label(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
    query: web::Query<RemoveLabelQuery>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    let data = service
        .issue_remove_label(&session, &wk, number, query.label_id)
        .await?;
    ok_json(data)
}

#[derive(Deserialize, utoipa::IntoParams)]
pub struct RemoveLabelQuery {
    pub label_id: Uuid,
}
