use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService,
    issues::{
        comment::{CreateComment, UpdateComment},
        types::IssueCommentResponse,
    },
};
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))
}

fn ok() -> Result<HttpResponse, ApiError> {
    Ok(HttpResponse::Ok().finish())
}

#[derive(Deserialize, utoipa::IntoParams)]
pub struct CommentPath {
    pub wk: String,
    pub number: i64,
    pub comment_id: Uuid,
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/issues/{number}/comments",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    request_body = CreateComment,
    responses(
        (status = 200, body = IssueCommentResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn create_comment(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
    params: web::Json<CreateComment>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    let data = service
        .issue_comment_create(&session, &wk, number, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/issues/{number}/comments",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    responses(
        (status = 200, body = Vec<IssueCommentResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn list_comments(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    let data = service.issue_comment_list(&session, &wk, number).await?;
    ok_json(data)
}
#[utoipa::path(
    put,
    path = "/api/v1/workspace/{wk}/issues/{number}/comments/{comment_id}",
    params(CommentPath),
    request_body = UpdateComment,
    responses(
        (status = 200, body = IssueCommentResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Comment not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn update_comment(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<CommentPath>,
    params: web::Json<UpdateComment>,
) -> Result<HttpResponse, ApiError> {
    let CommentPath {
        wk,
        number,
        comment_id,
    } = path.into_inner();
    let data = service
        .issue_comment_update(
            &session,
            &wk,
            number,
            comment_id,
            params.into_inner(),
        )
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/issues/{number}/comments/{comment_id}",
    params(CommentPath),
    responses(
        (status = 200, description = "Comment deleted"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Comment not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn delete_comment(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<CommentPath>,
) -> Result<HttpResponse, ApiError> {
    let CommentPath {
        wk,
        number,
        comment_id,
    } = path.into_inner();
    service
        .issue_comment_delete(&session, &wk, number, comment_id)
        .await?;
    ok()
}
