use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService,
    pull_request::{
        review::{CreatePrReview, CreatePrReviewComment, DismissPrReview},
        types::{PullRequestReviewCommentResponse, PullRequestReviewResponse},
    },
};
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 DismissPath {
    pub wk: String,
    pub repo: String,
    pub number: i64,
    pub review_id: Uuid,
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/repos/{repo}/pull-requests/{number}/reviews",
    params(PrPath),
    responses(
        (status = 200, body = Vec<PullRequestReviewResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    security(("session" = []))
)]
pub async fn list_reviews(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<PrPath>,
) -> Result<HttpResponse, ApiError> {
    let PrPath { wk, repo, number } = path.into_inner();
    let data = service.pr_review_list(&session, &wk, &repo, number).await?;
    ok_json(data)
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/repos/{repo}/pull-requests/{number}/reviews",
    params(PrPath),
    request_body = CreatePrReview,
    responses(
        (status = 200, body = PullRequestReviewResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "PR not found"),
    ),
    security(("session" = []))
)]
pub async fn create_review(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<PrPath>,
    params: web::Json<CreatePrReview>,
) -> Result<HttpResponse, ApiError> {
    let PrPath { wk, repo, number } = path.into_inner();
    let data = service
        .pr_review_create(&session, &wk, &repo, number, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/repos/{repo}/pull-requests/{number}/reviews/{review_id}/dismiss",
    params(DismissPath),
    request_body = DismissPrReview,
    responses(
        (status = 200, description = "Review dismissed"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Review not found"),
    ),
    security(("session" = []))
)]
pub async fn dismiss_review(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<DismissPath>,
    params: web::Json<DismissPrReview>,
) -> Result<HttpResponse, ApiError> {
    let DismissPath {
        wk,
        repo,
        number,
        review_id,
    } = path.into_inner();
    service
        .pr_review_dismiss(
            &session,
            &wk,
            &repo,
            number,
            review_id,
            params.into_inner(),
        )
        .await?;
    ok()
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/repos/{repo}/pull-requests/{number}/review-comments",
    params(PrPath),
    request_body = CreatePrReviewComment,
    responses(
        (status = 200, body = PullRequestReviewCommentResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "PR not found"),
    ),
    security(("session" = []))
)]
pub async fn create_review_comment(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<PrPath>,
    params: web::Json<CreatePrReviewComment>,
) -> Result<HttpResponse, ApiError> {
    let PrPath { wk, repo, number } = path.into_inner();
    let data = service
        .pr_review_comment_create(
            &session,
            &wk,
            &repo,
            number,
            None,
            params.into_inner(),
        )
        .await?;
    ok_json(data)
}
