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