use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService,
    issues::{reaction::AddReaction, types::IssueReactionResponse},
};
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))
}

#[derive(Deserialize)]
pub struct ReactionPath {
    pub wk: String,
    pub number: i64,
    pub comment_id: Option<Uuid>,
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/issues/{number}/reactions",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    request_body = AddReaction,
    responses(
        (status = 200, body = Vec<IssueReactionResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn add_reaction(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
    params: web::Json<AddReaction>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    let data = service
        .issue_add_reaction(&session, &wk, number, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/issues/{number}/reactions",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    responses(
        (status = 200, body = Vec<IssueReactionResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn remove_reaction(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
    query: web::Query<RemoveReactionQuery>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    let data = service
        .issue_remove_reaction(&session, &wk, number, &query.reaction)
        .await?;
    ok_json(data)
}

#[derive(Deserialize, utoipa::IntoParams)]
pub struct RemoveReactionQuery {
    pub reaction: String,
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/issues/{number}/comments/{comment_id}/reactions",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
        ("comment_id" = Uuid, Path, description = "Comment ID"),
    ),
    request_body = AddReaction,
    responses(
        (status = 200, body = Vec<IssueReactionResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Comment not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn add_comment_reaction(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64, Uuid)>,
    params: web::Json<AddReaction>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number, comment_id) = path.into_inner();
    let data = service
        .issue_comment_add_reaction(
            &session,
            &wk,
            number,
            comment_id,
            params.into_inner(),
        )
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/issues/{number}/comments/{comment_id}/reactions",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
        ("comment_id" = Uuid, Path, description = "Comment ID"),
    ),
    responses(
        (status = 200, body = Vec<IssueReactionResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn remove_comment_reaction(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64, Uuid)>,
    query: web::Query<RemoveReactionQuery>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number, comment_id) = path.into_inner();
    let data = service
        .issue_comment_remove_reaction(
            &session,
            &wk,
            number,
            comment_id,
            &query.reaction,
        )
        .await?;
    ok_json(data)
}
