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}/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())
}
