use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use service::pull_request::review_request::ReviewRequestCreateRequest;
use session::Session;
use uuid::Uuid;

#[utoipa::path(
    get,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/review-requests",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
    ),
    responses(
        (status = 200, description = "List review requests for a pull request", body = ApiResponse<service::pull_request::ReviewRequestListResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
    ),
    tag = "PullRequest"
)]
pub async fn review_request_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number) = path.into_inner();
    let resp = service
        .review_request_list(namespace, repo, pr_number, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/review-requests",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
    ),
    request_body = ReviewRequestCreateRequest,
    responses(
        (status = 200, description = "Create or update a review request", body = ApiResponse<service::pull_request::ReviewRequestResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "PullRequest"
)]
pub async fn review_request_create(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64)>,
    body: web::Json<ReviewRequestCreateRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number) = path.into_inner();
    let resp = service
        .review_request_create(namespace, repo, pr_number, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/review-requests/{reviewer}",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
        ("reviewer" = Uuid, Path),
    ),
    responses(
        (status = 200, description = "Delete (cancel) a review request"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "PullRequest"
)]
pub async fn review_request_delete(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64, Uuid)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number, reviewer) = path.into_inner();
    service
        .review_request_delete(namespace, repo, pr_number, reviewer, &session)
        .await?;
    Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}

#[utoipa::path(
    post,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/review-requests/{reviewer}/dismiss",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
        ("reviewer" = Uuid, Path),
    ),
    responses(
        (status = 200, description = "Dismiss a review request", body = ApiResponse<service::pull_request::ReviewRequestResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "PullRequest"
)]
pub async fn review_request_dismiss(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64, Uuid)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number, reviewer) = path.into_inner();
    let resp = service
        .review_request_dismiss(namespace, repo, pr_number, reviewer, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
