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

use super::ListQuery;

#[utoipa::path(
    get,
    path = "/api/repo_pr/{namespace}/{repo}/pulls",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("status" = Option<String>, Query),
        ("page" = Option<i64>, Query),
        ("per_page" = Option<i64>, Query),
    ),
    responses(
        (status = 200, description = "List pull requests", body = ApiResponse<service::pull_request::PullRequestListResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "PullRequest"
)]
pub async fn pull_request_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String)>,
    query: web::Query<ListQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo) = path.into_inner();
    let resp = service
        .pull_request_list(
            namespace,
            repo,
            query.status.clone(),
            Some(query.page.unwrap_or(1)),
            Some(query.per_page.unwrap_or(20)),
            &session,
        )
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

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

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

#[utoipa::path(
    patch,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{number}",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("number" = i64, Path),
    ),
    request_body = service::pull_request::PullRequestUpdateRequest,
    responses(
        (status = 200, description = "Update pull request", body = ApiResponse<service::pull_request::PullRequestResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "PullRequest"
)]
pub async fn pull_request_update(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64)>,
    body: web::Json<service::pull_request::PullRequestUpdateRequest>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, number) = path.into_inner();
    let resp = service
        .pull_request_update(namespace, repo, number, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

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

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

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

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

#[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())
}

#[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(
    get,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/merge",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
    ),
    responses(
        (status = 200, description = "Get merge analysis", body = ApiResponse<service::pull_request::MergeAnalysisResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "PullRequest"
)]
pub async fn merge_analysis(
    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
        .merge_analysis(namespace, repo, pr_number, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/conflicts",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
    ),
    responses(
        (status = 200, description = "Check merge conflicts", body = ApiResponse<service::pull_request::MergeConflictResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "PullRequest"
)]
pub async fn merge_conflict_check(
    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
        .merge_conflict_check(namespace, repo, pr_number, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

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

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

#[utoipa::path(
    get,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/merge/in_progress",
    params(
        ("namespace" = String, Path),
        ("repo" = String, Path),
        ("pr_number" = i64, Path),
    ),
    responses(
        (status = 200, description = "Check if merge is in progress"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "PullRequest"
)]
pub async fn merge_is_in_progress(
    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
        .merge_is_in_progress(namespace, repo, pr_number, &session)
        .await?;
    Ok(ApiResponse::ok(serde_json::json!({ "in_progress": resp })).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/diff/side-by-side",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("pr_number" = i64, Path, description = "Pull request number"),
    ),
    responses(
        (status = 200, description = "Side-by-side diff for a pull request", body = ApiResponse<service::git::diff::SideBySideDiffResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
    ),
    tag = "PullRequest"
)]
pub async fn pr_diff_side_by_side(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, String, i64)>,
    query: web::Query<SideBySideDiffQuery>,
) -> Result<HttpResponse, ApiError> {
    let (namespace, repo, pr_number) = path.into_inner();
    let resp = service
        .pr_diff_side_by_side(namespace, repo, pr_number, query.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/commits",
    params(
        ("namespace" = String, Path, description = "Project namespace"),
        ("repo" = String, Path, description = "Repository name"),
        ("pr_number" = i64, Path, description = "Pull request number"),
    ),
    responses(
        (status = 200, description = "List commits in a pull request", body = ApiResponse<service::pull_request::PrCommitsListResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
    ),
    tag = "PullRequest"
)]
pub async fn pr_commits_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
        .pr_commits_list(namespace, repo, pr_number, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
