// 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 pib_service_api_types::config::oidc::OidcConfig;
use pib_service_inventory::InventoryProvider;
use tokio::net::ToSocketAddrs;

mod error;
mod oidc;
mod router;

use router::router;

pub use error::Error;
use oidc::OidcAuth;

pub type Result<T, E = Error> = std::result::Result<T, E>;

struct ServiceState {
    pub inventory_provider: Arc<dyn InventoryProvider>,
    pub oidc: OidcConfig,
}

pub async fn run<A: ToSocketAddrs>(
    inventory_provider: Arc<dyn InventoryProvider>,
    addrs: A,
    oidc: OidcConfig,
) -> anyhow::Result<()> {
    let service_state = ServiceState {
        inventory_provider,
        oidc,
    };
    let app = router(service_state);
    let listener = tokio::net::TcpListener::bind(addrs).await?;
    axum::serve(listener, app).await?;
    Ok(())
}
