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

#[utoipa::path(
    get,
    path = "/api/issue/{project}/issues/{number}/pulls",
    params(
        ("project" = String, Path),
        ("number" = i64, Path),
    ),
    responses(
        (status = 200, description = "List issue pull requests", body = ApiResponse<Vec<service::issue::IssuePullRequestResponse>>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Issues"
)]
pub async fn issue_pull_request_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (project, issue_number) = path.into_inner();
    let resp = service
        .issue_pull_request_list(project, issue_number, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/issue/{project}/issues/{number}/pulls",
    params(
        ("project" = String, Path),
        ("number" = i64, Path),
    ),
    request_body = service::issue::IssueLinkPullRequestRequest,
    responses(
        (status = 200, description = "Link pull request to issue", body = ApiResponse<service::issue::IssuePullRequestResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "Issues"
)]
pub async fn issue_pull_request_link(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, i64)>,
    body: web::Json<service::issue::IssueLinkPullRequestRequest>,
) -> Result<HttpResponse, ApiError> {
    let (project, issue_number) = path.into_inner();
    let resp = service
        .issue_pull_request_link(project, issue_number, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/issue/{project}/issues/{number}/pulls/{repo_id}/{pr_number}",
    params(
        ("project" = String, Path),
        ("number" = i64, Path),
        ("repo_id" = String, Path),
        ("pr_number" = i64, Path),
    ),
    responses(
        (status = 200, description = "Unlink pull request from issue"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "Issues"
)]
pub async fn issue_pull_request_unlink(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, i64, String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (project, issue_number, repo_id, pr_number) = path.into_inner();
    let repo_uuid = uuid::Uuid::parse_str(&repo_id)
        .map_err(|_| service::error::AppError::BadRequest("Invalid UUID".to_string()))?;
    service
        .issue_pull_request_unlink(project, issue_number, repo_uuid, pr_number, &session)
        .await?;
    Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}
