use actix_web::{HttpResponse, web};
use serde::Serialize;
use service::{AppService, issues::types::IssueEventResponse};
use session::Session;

use crate::error::ApiError;

fn ok_json<T: Serialize>(data: T) -> Result<HttpResponse, ApiError> {
    Ok(HttpResponse::Ok().json(data))
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/issues/{number}/events",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    responses(
        (status = 200, body = Vec<IssueEventResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn list_events(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    let data = service.issue_events(&session, &wk, number).await?;
    ok_json(data)
}
