//! DreamStack Bitstream Relay Server
//!
//! Usage: `cargo run -p ds-stream [OPTIONS]`
//!
//! Starts a WebSocket relay on port 9100 (default).
//! - Source connects to ws://localhost:9100/source
//! - Receivers connect to ws://localhost:9100/stream

use ds_stream::relay::{run_relay, RelayConfig};

#[tokio::main]
async fn main() {
    let args: Vec<String> = std::env::args().collect();

    let mut config = RelayConfig::default();

    // Parse arguments: --port, --replay-depth, --record, --federate, --max-receivers, --max-channels
    let mut i = 1;
    while i < args.len() {
        match args[i].as_str() {
            "--port" | "-p" => {
                i += 1;
                if let Some(val) = args.get(i) {
                    if let Ok(port) = val.parse::<u16>() {
                        config.addr = format!("0.0.0.0:{}", port).parse().unwrap();
                    }
                }
            }
            "--replay-depth" => {
                i += 1;
                if let Some(val) = args.get(i) {
                    if let Ok(depth) = val.parse::<usize>() {
                        config.replay_depth = depth;
                    }
                }
            }
            "--record" => {
                i += 1;
                if let Some(val) = args.get(i) {
                    config.recording_dir = Some(val.clone());
                }
            }
            "--federate" => {
                i += 1;
                if let Some(val) = args.get(i) {
                    config.federation_upstreams.push(val.clone());
                }
            }
            "--max-receivers" => {
                i += 1;
                if let Some(val) = args.get(i) {
                    if let Ok(n) = val.parse::<usize>() {
                        config.max_receivers = n;
                    }
                }
            }
            "--max-channels" => {
                i += 1;
                if let Some(val) = args.get(i) {
                    if let Ok(n) = val.parse::<usize>() {
                        config.max_channels = n;
                    }
                }
            }
            "--help" | "-h" => {
                eprintln!("DreamStack Bitstream Relay Server\n");
                eprintln!("USAGE:");
                eprintln!("    ds-stream [OPTIONS]\n");
                eprintln!("OPTIONS:");
                eprintln!("    -p, --port <PORT>              Port to listen on (default: 9100)");
                eprintln!("    --replay-depth <N>             Frames to keep in replay buffer (default: 0 = disabled)");
                eprintln!("    --record <DIR>                 Directory to record frames to (.dsrec files)");
                eprintln!("    --federate <URL>               Upstream relay URL (can be repeated)");
                eprintln!("    --max-receivers <N>            Max receivers per channel (default: 64)");
                eprintln!("    --max-channels <N>             Max channels (default: 256)");
                eprintln!("    -h, --help                     Print help");
                std::process::exit(0);
            }
            other => {
                // Legacy: first positional arg = port
                if let Ok(port) = other.parse::<u16>() {
                    config.addr = format!("0.0.0.0:{}", port).parse().unwrap();
                } else {
                    eprintln!("Unknown argument: {other}. Use --help for usage.");
                    std::process::exit(1);
                }
            }
        }
        i += 1;
    }

    eprintln!("Starting DreamStack Bitstream Relay on {}...", config.addr);
    if let Err(e) = run_relay(config).await {
        eprintln!("Relay error: {}", e);
        std::process::exit(1);
    }
}
