use actix_web::{HttpResponse, web};
use serde::Serialize;
use service::{
    AppService,
    auth::captcha::{CaptchaQuery, CaptchaResponse},
};
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/captcha",
    params(CaptchaQuery),
    responses((status = 200, body = CaptchaResponse)),
    tag = "auth"
)]
pub async fn captcha(
    session: Session,
    query: web::Query<CaptchaQuery>,
    service: web::Data<AppService>,
) -> Result<HttpResponse, ApiError> {
    let result = service.auth_captcha(&session, query.into_inner()).await?;
    ok_json(result)
}
