// SPDX-FileCopyrightText: Politik im Blick developers
// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
//
// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2

use std::sync::Arc;

use axum::{Extension, Json, extract::State};
use pib_service_api_types::me::profile::GetResponseBody;

use crate::{Result, ServiceState, oidc::UserInfo};

pub(crate) async fn get(
    State(service_state): State<Arc<ServiceState>>,
    Extension(user): Extension<UserInfo>,
) -> Result<Json<GetResponseBody>> {
    let mut inventory = service_state.inventory_provider.get_inventory().await?;

    let user = inventory.get_user_by_sub(user.sub.to_string()).await?;

    Ok(Json(GetResponseBody {
        display_name: user.display_name,
    }))
}
