use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService,
    pull_request::{
        reaction::AddPrReaction, types::PullRequestReactionResponse,
    },
};
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 PrPath {
    pub wk: String,
    pub repo: String,
    pub number: i64,
}

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

#[derive(Deserialize, utoipa::IntoParams)]
pub struct CommentReactionPath {
    pub wk: String,
    pub repo: String,
    pub number: i64,
    pub comment_id: Uuid,
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/repos/{repo}/pull-requests/{number}/reactions",
    params(PrPath),
    request_body = AddPrReaction,
    responses(
        (status = 200, body = PullRequestReactionResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "PR not found"),
    ),
    security(("session" = []))
)]
pub async fn add_reaction(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<PrPath>,
    params: web::Json<AddPrReaction>,
) -> Result<HttpResponse, ApiError> {
    let PrPath { wk, repo, number } = path.into_inner();
    let data = service
        .pr_add_reaction(&session, &wk, &repo, number, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/repos/{repo}/pull-requests/{number}/reactions",
    params(PrPath),
    responses(
        (status = 200, description = "Reaction removed"),
        (status = 401, description = "Unauthorized"),
    ),
    security(("session" = []))
)]
pub async fn remove_reaction(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<PrPath>,
    query: web::Query<RemoveReactionQuery>,
) -> Result<HttpResponse, ApiError> {
    let PrPath { wk, repo, number } = path.into_inner();
    service
        .pr_remove_reaction(&session, &wk, &repo, number, query.reaction_id)
        .await?;
    ok()
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/repos/{repo}/pull-requests/{number}/comments/{comment_id}/reactions",
    params(CommentReactionPath),
    request_body = AddPrReaction,
    responses(
        (status = 200, body = PullRequestReactionResponse),
        (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<CommentReactionPath>,
    params: web::Json<AddPrReaction>,
) -> Result<HttpResponse, ApiError> {
    let CommentReactionPath {
        wk,
        repo,
        number,
        comment_id,
    } = path.into_inner();
    let data = service
        .pr_comment_add_reaction(
            &session,
            &wk,
            &repo,
            number,
            comment_id,
            params.into_inner(),
        )
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/repos/{repo}/pull-requests/{number}/comments/{comment_id}/reactions",
    params(CommentReactionPath),
    responses(
        (status = 200, description = "Reaction removed"),
        (status = 401, description = "Unauthorized"),
    ),
    security(("session" = []))
)]
pub async fn remove_comment_reaction(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<CommentReactionPath>,
    query: web::Query<RemoveReactionQuery>,
) -> Result<HttpResponse, ApiError> {
    let CommentReactionPath {
        wk,
        repo,
        number,
        comment_id: _,
    } = path.into_inner();
    service
        .pr_comment_remove_reaction(
            &session,
            &wk,
            &repo,
            number,
            query.reaction_id,
        )
        .await?;
    ok()
}
