use actix_web::{HttpResponse, web};
use serde::Serialize;
use service::{AppService, auth::rsa::RsaResponse};
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/auth/public-key",
    responses((status = 200, body = RsaResponse)),
    tag = "auth"
)]
pub async fn rsa(
    session: Session,
    service: web::Data<AppService>,
) -> Result<HttpResponse, ApiError> {
    let result = service.auth_rsa(&session).await?;
    ok_json(result)
}
