use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService,
    issues::{
        binding::{BindIssuePullRequest, BindIssueRepo},
        types::{IssuePullRequestResponse, IssueRepoResponse},
    },
};
use session::Session;
use uuid::Uuid;

use crate::error::ApiError;

fn ok_json<T: Serialize>(data: T) -> Result<HttpResponse, ApiError> {
    Ok(HttpResponse::Ok().json(data))
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/issues/{number}/repos",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    request_body = BindIssueRepo,
    responses(
        (status = 200, body = Vec<IssueRepoResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue or repo not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn bind_repo(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
    params: web::Json<BindIssueRepo>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    let data = service
        .issue_bind_repo(&session, &wk, number, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/issues/{number}/repos",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    responses(
        (status = 200, body = Vec<IssueRepoResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue or repo not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn unbind_repo(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
    query: web::Query<UnbindRepoQuery>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    let data = service
        .issue_unbind_repo(&session, &wk, number, query.repo_id)
        .await?;
    ok_json(data)
}

#[derive(Deserialize, utoipa::IntoParams)]
pub struct UnbindRepoQuery {
    pub repo_id: Uuid,
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/issues/{number}/pull-requests",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    request_body = BindIssuePullRequest,
    responses(
        (status = 200, body = Vec<IssuePullRequestResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue or PR not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn bind_pull_request(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
    params: web::Json<BindIssuePullRequest>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    let data = service
        .issue_bind_pull_request(&session, &wk, number, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/issues/{number}/pull-requests",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    responses(
        (status = 200, body = Vec<IssuePullRequestResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue or PR not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn unbind_pull_request(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
    query: web::Query<UnbindPrQuery>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    let data = service
        .issue_unbind_pull_request(&session, &wk, number, query.pull_request_id)
        .await?;
    ok_json(data)
}

#[derive(Deserialize, utoipa::IntoParams)]
pub struct UnbindPrQuery {
    pub pull_request_id: Uuid,
}
