use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService,
    pull_request::{
        comment::{CreatePrComment, UpdatePrComment},
        types::PullRequestCommentResponse,
    },
};
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 PrPath {
    pub wk: String,
    pub repo: String,
    pub number: i64,
}

#[derive(Deserialize, utoipa::IntoParams)]
pub struct CommentPath {
    pub wk: String,
    pub repo: String,
    pub number: i64,
    pub comment_id: Uuid,
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/repos/{repo}/pull-requests/{number}/comments",
    params(PrPath),
    responses(
        (status = 200, body = Vec<PullRequestCommentResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    security(("session" = []))
)]
pub async fn list_comments(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<PrPath>,
) -> Result<HttpResponse, ApiError> {
    let PrPath { wk, repo, number } = path.into_inner();
    let data = service
        .pr_comment_list(&session, &wk, &repo, number)
        .await?;
    ok_json(data)
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/repos/{repo}/pull-requests/{number}/comments",
    params(PrPath),
    request_body = CreatePrComment,
    responses(
        (status = 200, body = PullRequestCommentResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "PR not found"),
    ),
    security(("session" = []))
)]
pub async fn create_comment(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<PrPath>,
    params: web::Json<CreatePrComment>,
) -> Result<HttpResponse, ApiError> {
    let PrPath { wk, repo, number } = path.into_inner();
    let data = service
        .pr_comment_create(&session, &wk, &repo, number, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    put,
    path = "/api/v1/workspace/{wk}/repos/{repo}/pull-requests/{number}/comments/{comment_id}",
    params(CommentPath),
    request_body = UpdatePrComment,
    responses(
        (status = 200, body = PullRequestCommentResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Only author can update"),
        (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<UpdatePrComment>,
) -> Result<HttpResponse, ApiError> {
    let CommentPath {
        wk,
        repo,
        number,
        comment_id,
    } = path.into_inner();
    let data = service
        .pr_comment_update(
            &session,
            &wk,
            &repo,
            number,
            comment_id,
            params.into_inner(),
        )
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/repos/{repo}/pull-requests/{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,
        repo,
        number: _,
        comment_id,
    } = path.into_inner();
    service
        .pr_comment_delete(&session, &wk, &repo, comment_id)
        .await?;
    ok()
}
