use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use session::Session;

#[utoipa::path(
    get,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/reviews",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
    ),
    responses(
        (status = 200, description = "List pull request reviews", body = ApiResponse<service::pull_request::ReviewListResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "PullRequest"
)]
pub async fn review_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_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}/reviews",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
    ),
    request_body = service::pull_request::ReviewSubmitRequest,
    responses(
        (status = 200, description = "Submit pull request review", body = ApiResponse<service::pull_request::ReviewResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "PullRequest"
)]
pub async fn review_submit(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64)>,
    body: web::Json<service::pull_request::ReviewSubmitRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number) = path.into_inner();
    let resp = service
        .review_submit(namespace, repo, pr_number, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    patch,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/reviews",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
    ),
    request_body = service::pull_request::ReviewUpdateRequest,
    responses(
        (status = 200, description = "Update pull request review", body = ApiResponse<service::pull_request::ReviewResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "PullRequest"
)]
pub async fn review_update(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64)>,
    body: web::Json<service::pull_request::ReviewUpdateRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number) = path.into_inner();
    let resp = service
        .review_update(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}/reviews/{reviewer_id}",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
        ("reviewer_id" = String, Path, description = "Reviewer UUID"),
    ),
    responses(
        (status = 200, description = "Delete pull request review"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "PullRequest"
)]
pub async fn review_delete(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64, String)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number, reviewer_id) = path.into_inner();
    let reviewer_uuid = uuid::Uuid::parse_str(&reviewer_id)
        .map_err(|_| service::error::AppError::BadRequest("Invalid reviewer ID".to_string()))?;
    service
        .review_delete(namespace, repo, pr_number, reviewer_uuid, &session)
        .await?;
    Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}
