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

#[utoipa::path(
    get,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/comments",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
        ("path" = Option<String>, Query, description = "Filter by file path"),
        ("resolved" = Option<bool>, Query, description = "Filter by resolved status"),
        ("file_only" = Option<bool>, Query, description = "Only inline comments (true) or only general comments (false)"),
    ),
    responses(
        (status = 200, description = "List pull request review comments", body = ApiResponse<service::pull_request::ReviewCommentListResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "PullRequest"
)]
pub async fn review_comment_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64)>,
    query: web::Query<ReviewCommentListQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number) = path.into_inner();
    let resp = service
        .review_comment_list(namespace, repo, pr_number, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/comments",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
    ),
    request_body = service::pull_request::ReviewCommentCreateRequest,
    responses(
        (status = 200, description = "Create pull request review comment", body = ApiResponse<service::pull_request::ReviewCommentResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "PullRequest"
)]
pub async fn review_comment_create(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64)>,
    body: web::Json<service::pull_request::ReviewCommentCreateRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number) = path.into_inner();
    let resp = service
        .review_comment_create(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}/comments/{comment_id}",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
        ("comment_id" = i64, Path),
    ),
    request_body = service::pull_request::ReviewCommentUpdateRequest,
    responses(
        (status = 200, description = "Update pull request review comment", body = ApiResponse<service::pull_request::ReviewCommentResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "PullRequest"
)]
pub async fn review_comment_update(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64, i64)>,
    body: web::Json<service::pull_request::ReviewCommentUpdateRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number, comment_id) = path.into_inner();
    let resp = service
        .review_comment_update(
            namespace,
            repo,
            pr_number,
            comment_id,
            body.into_inner(),
            &session,
        )
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

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

#[utoipa::path(
    put,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/comments/{comment_id}/resolve",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
        ("comment_id" = i64, Path),
    ),
    responses(
        (status = 200, description = "Mark comment as resolved", body = ApiResponse<service::pull_request::review_comment::ReviewCommentResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "PullRequest"
)]
pub async fn review_comment_resolve(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number, comment_id) = path.into_inner();
    let resp = service
        .review_comment_resolve(namespace, repo, pr_number, comment_id, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/comments/{comment_id}/resolve",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
        ("comment_id" = i64, Path),
    ),
    responses(
        (status = 200, description = "Mark comment as unresolved", body = ApiResponse<service::pull_request::review_comment::ReviewCommentResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "PullRequest"
)]
pub async fn review_comment_unresolve(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number, comment_id) = path.into_inner();
    let resp = service
        .review_comment_unresolve(namespace, repo, pr_number, comment_id, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/comments/{comment_id}/replies",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
        ("comment_id" = i64, Path),
    ),
    request_body = service::pull_request::review_comment::ReviewCommentReplyRequest,
    responses(
        (status = 200, description = "Reply to a comment", body = ApiResponse<service::pull_request::review_comment::ReviewCommentResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "PullRequest"
)]
pub async fn review_comment_reply(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64, i64)>,
    body: web::Json<ReviewCommentReplyRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number, comment_id) = path.into_inner();
    let resp = service
        .review_comment_reply(
            namespace,
            repo,
            pr_number,
            comment_id,
            body.into_inner(),
            &session,
        )
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
