use actix_web::{HttpResponse, web};
use serde::Serialize;
use service::{AppService, auth::register::RegisterParams};
use session::Session;

use crate::error::ApiError;

fn ok_json<T: Serialize>(data: T) -> Result<HttpResponse, ApiError> {
    Ok(HttpResponse::Ok().json(data))
}

#[utoipa::path(
    post,
    path = "/api/v1/auth/register",
    request_body = RegisterParams,
    responses((status = 200)),
    tag = "auth"
)]
pub async fn register(
    session: Session,
    params: web::Json<RegisterParams>,
    service: web::Data<AppService>,
) -> Result<HttpResponse, ApiError> {
    let result = service.auth_register(params.into_inner(), &session).await?;
    ok_json(result)
}
