mod config;
mod relay;
mod proto;

use anyhow::Result;
use config::Config;
use relay::Relay;
use tracing::{info, error};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize tracing
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| "relay=info".into()),
        )
        .with(tracing_subscriber::fmt::layer())
        .init();

    info!("Starting Stateless ZMQ Relay Gateway");
    info!("Architecture: Async event-driven with pub/sub notifications");

    // Load configuration
    let config = Config::from_env()?;
    info!("Configuration loaded: {:?}", config);

    // Create and run stateless relay
    let relay = Relay::new(config)?;

    // Handle shutdown signals
    tokio::select! {
        result = relay.run() => {
            match result {
                Ok(_) => info!("Relay stopped gracefully"),
                Err(e) => error!("Relay error: {}", e),
            }
        }
        _ = tokio::signal::ctrl_c() => {
            info!("Received shutdown signal");
        }
    }

    info!("ZMQ Relay Gateway stopped");
    Ok(())
}
