// 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::{Json, extract::State};
use pib_service_api_types::body::{Body, GetBodyListResponseBody};

use crate::{Result, ServiceState};
pub(crate) mod by_body_id;

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

    let data = bodies
        .into_iter()
        .map(
            |pib_service_inventory::Body {
                 id,
                 created,
                 modified,
                 short_name,
                 name,
                 website,
                 license,
                 license_valid_since,
                 oparl_since,
                 ags,
                 rgs,
                 contact_email,
                 contact_name,
                 classification,
             }| Body {
                id,
                created,
                modified,
                short_name,
                name,
                website,
                license,
                license_valid_since,
                oparl_since,
                ags,
                rgs,
                contact_email,
                contact_name,
                classification,
            },
        )
        .collect();

    Ok(Json(GetBodyListResponseBody { data }))
}
