#[cfg(feature = "ssr")]
#[allow(clippy::needless_return)]
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    use avam::config::Config;
    use avam::domain::api;
    use avam::inbound::http::{state::AppState, HttpServer};
    use avam::outbound::dangerous_lettre::DangerousLettre;
    use avam::outbound::postgres::Postgres;
    use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

    dotenvy::dotenv().ok();

    tracing_subscriber::registry()
        .with(tracing_subscriber::EnvFilter::try_from_default_env()?)
        .with(tracing_subscriber::fmt::layer())
        .init();

    let config = Config::from_env()?;

    // outbound
    let postgres = Postgres::new(&config.database_url).await?;
    let dangerous_lettre = DangerousLettre::new(
        &config.smtp_host,
        config.smtp_port,
        &config.smtp_username,
        &config.smtp_password,
        &config.smtp_sender,
    )?;

    let api_service = api::Service::new(postgres.clone(), dangerous_lettre);

    let app_state = AppState::new(api_service, config.clone()).await;

    HttpServer::new(app_state, postgres.pool())
        .await?
        .run()
        .await
}

#[cfg(not(feature = "ssr"))]
pub fn main() {
    // no client-side main function
    // unless we want this to work with e.g., Trunk for a purely client-side app
    // see lib.rs for hydration function instead
}
