//! stygian-api binary — REST API server for pipeline management
//!
//! Reads the following environment variables:
//!
//! | Variable | Default | Description |
//! | ---------- | --------- | ------------- |
//! | `STYGIAN_API_KEY` | `"dev-key"` | API key for `/pipelines` routes |
//! | `STYGIAN_BIND` | `"0.0.0.0:8080"` | TCP address to listen on |
//! | `RUST_LOG` | `"info"` | Tracing filter |
//!
//! # Usage
//!
//! ```bash
//! STYGIAN_API_KEY=secret cargo run --bin stygian-api
//! curl -H "X-Api-Key: secret" http://localhost:8080/pipelines
//! ```

use stygian_graph::application::api_server::ApiServer;

fn maybe_print_version_and_exit() -> bool {
    match std::env::args().nth(1).as_deref() {
        Some("--version" | "-V") => {
            println!("stygian-api {}", env!("CARGO_PKG_VERSION"));
            true
        }
        _ => false,
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    if maybe_print_version_and_exit() {
        return Ok(());
    }

    // Initialise tracing
    let log_filter = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string());
    tracing_subscriber::fmt()
        .with_env_filter(log_filter)
        .compact()
        .init();

    let bind = std::env::var("STYGIAN_BIND").unwrap_or_else(|_| "0.0.0.0:8080".to_string());

    ApiServer::from_env().run(&bind).await
}
