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}/comments",
    params(
        ("project" = String, Path),
        ("number" = i64, Path),
        ("page" = Option<i64>, Query),
        ("per_page" = Option<i64>, Query),
    ),
    responses(
        (status = 200, description = "List issue comments", body = ApiResponse<service::issue::IssueCommentListResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Issues"
)]
pub async fn issue_comment_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, i64)>,
    query: web::Query<super::PagerQuery>,
) -> Result<HttpResponse, ApiError> {
    let (project, issue_number) = path.into_inner();
    let resp = service
        .issue_comment_list(
            project,
            issue_number,
            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}/comments/{comment_id}",
    params(
        ("project" = String, Path),
        ("number" = i64, Path),
        ("comment_id" = i64, Path),
    ),
    responses(
        (status = 200, description = "Get issue comment", body = ApiResponse<service::issue::IssueCommentResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Issues"
)]
pub async fn issue_comment_get(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, i64, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (project, issue_number, comment_id) = path.into_inner();
    let resp = service
        .issue_comment_get(project, issue_number, comment_id, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

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

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

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