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

pub mod assignee;
pub mod comment;
pub mod comment_reaction;
pub mod issue_label;
pub mod label;
pub mod pull_request;
pub mod reaction;
pub mod repo;
pub mod subscriber;

#[derive(serde::Deserialize, utoipa::IntoParams)]
pub struct ListQuery {
    pub state: Option<String>,
    pub page: Option<i64>,
    pub per_page: Option<i64>,
}

#[derive(serde::Deserialize, utoipa::IntoParams)]
pub struct PagerQuery {
    pub page: Option<i64>,
    pub per_page: Option<i64>,
}

#[utoipa::path(
    get,
    path = "/api/issue/{project}/issues",
    params(
        ("project" = String, Path),
        ("state" = Option<String>, Query),
        ("page" = Option<i64>, Query),
        ("per_page" = Option<i64>, Query),
    ),
    responses(
        (status = 200, description = "List issues", body = ApiResponse<service::issue::IssueListResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Issues"
)]
pub async fn issue_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
    query: web::Query<ListQuery>,
) -> Result<HttpResponse, ApiError> {
    let project = path.into_inner();
    let resp = service
        .issue_list(
            project,
            query.state.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/issue/{project}/issues/{number}",
    params(
        ("project" = String, Path),
        ("number" = i64, Path),
    ),
    responses(
        (status = 200, description = "Get issue", body = ApiResponse<service::issue::IssueResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Issues"
)]
pub async fn issue_get(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (project, number) = path.into_inner();
    let resp = service.issue_get(project, number, &session).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

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

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

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

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

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

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

pub fn init_issue_routes(cfg: &mut web::ServiceConfig) {
    cfg.service(
        web::scope("issue/{project}")
            .route("/issues", web::get().to(issue_list))
            .route("/issues", web::post().to(issue_create))
            .route("/issues/summary", web::get().to(issue_summary))
            .route("/issues/{number}", web::get().to(issue_get))
            .route("/issues/{number}", web::patch().to(issue_update))
            .route("/issues/{number}", web::delete().to(issue_delete))
            .route("/issues/{number}/close", web::post().to(issue_close))
            .route("/issues/{number}/reopen", web::post().to(issue_reopen))
            .route(
                "/issues/{number}/labels",
                web::get().to(issue_label::issue_label_list),
            )
            .route(
                "/issues/{number}/labels",
                web::post().to(issue_label::issue_label_add),
            )
            .route(
                "/issues/{number}/labels/bulk",
                web::post().to(issue_label::issue_label_add_bulk),
            )
            .route(
                "/issues/{number}/labels/{label_id}",
                web::delete().to(issue_label::issue_label_remove),
            )
            .route(
                "/issues/{number}/comments",
                web::get().to(comment::issue_comment_list),
            )
            .route(
                "/issues/{number}/comments/{comment_id}",
                web::get().to(comment::issue_comment_get),
            )
            .route(
                "/issues/{number}/comments",
                web::post().to(comment::issue_comment_create),
            )
            .route(
                "/issues/{number}/comments/{comment_id}",
                web::patch().to(comment::issue_comment_update),
            )
            .route(
                "/issues/{number}/comments/{comment_id}",
                web::delete().to(comment::issue_comment_delete),
            )
            .route(
                "/issues/{number}/comments/{comment_id}/reactions",
                web::get().to(comment_reaction::issue_comment_reaction_list),
            )
            .route(
                "/issues/{number}/comments/{comment_id}/reactions",
                web::post().to(comment_reaction::issue_comment_reaction_add),
            )
            .route(
                "/issues/{number}/comments/{comment_id}/reactions/{reaction}",
                web::delete().to(comment_reaction::issue_comment_reaction_remove),
            )
            .route(
                "/issues/{number}/assignees",
                web::get().to(assignee::issue_assignee_list),
            )
            .route(
                "/issues/{number}/assignees",
                web::post().to(assignee::issue_assignee_add),
            )
            .route(
                "/issues/{number}/assignees/{assignee_id}",
                web::delete().to(assignee::issue_assignee_remove),
            )
            .route(
                "/issues/{number}/subscribers",
                web::get().to(subscriber::issue_subscriber_list),
            )
            .route(
                "/issues/{number}/subscribe",
                web::post().to(subscriber::issue_subscribe),
            )
            .route(
                "/issues/{number}/subscribe",
                web::delete().to(subscriber::issue_unsubscribe),
            )
            .route(
                "/issues/{number}/reactions",
                web::get().to(reaction::issue_reaction_list),
            )
            .route(
                "/issues/{number}/reactions",
                web::post().to(reaction::issue_reaction_add),
            )
            .route(
                "/issues/{number}/reactions/{reaction}",
                web::delete().to(reaction::issue_reaction_remove),
            )
            .route(
                "/issues/{number}/repos",
                web::get().to(repo::issue_repo_list),
            )
            .route(
                "/issues/{number}/repos",
                web::post().to(repo::issue_repo_link),
            )
            .route(
                "/issues/{number}/repos/{repo_id}",
                web::delete().to(repo::issue_repo_unlink),
            )
            .route(
                "/issues/{number}/pulls",
                web::get().to(pull_request::issue_pull_request_list),
            )
            .route(
                "/issues/{number}/pulls",
                web::post().to(pull_request::issue_pull_request_link),
            )
            .route(
                "/issues/{number}/pulls/{repo_id}/{pr_number}",
                web::delete().to(pull_request::issue_pull_request_unlink),
            )
            // labels (at project level)
            .route("/labels", web::get().to(label::label_list))
            .route("/labels", web::post().to(label::label_create))
            .route("/labels/{label_id}", web::delete().to(label::label_delete)),
    );
}
