//! Standalone entrypoint for the Pulse Relay.
//!
//! This is a thin wrapper that parses CLI arguments via clap and
//! delegates to [`pulse_relay::run`]. The same `run` function is
//! also called by `pulse-cli serve`, making this binary a
//! backward-compatible alias.

use clap::Parser;
use pulse_relay::{Mode, QueueStorage, ServeArgs};

#[derive(Parser, Debug, Clone)]
#[command(author, version, about = "Pulse — Unified Real-Time Server")]
struct Args {
    /// Deployment mode: standalone (default), relay, or control.
    #[arg(long, value_enum, default_value_t = Mode::Standalone)]
    mode: Mode,

    #[arg(short, long, default_value = "0.0.0.0:4001")]
    bind: String,

    #[arg(short, long, default_value = "localhost:4001")]
    public_endpoint: String,

    #[arg(short, long, default_value = "eu-west-1")]
    region: String,

    #[arg(long, default_value_t = 0.0)]
    lat: f64,

    #[arg(long, default_value_t = 0.0)]
    lon: f64,

    /// Control plane URL (only used in --mode relay).
    #[arg(long, default_value = "http://localhost:4002")]
    control_url: String,

    #[arg(long, default_value_t = 100000)]
    capacity: u32,

    #[arg(long, env = "AI_STT_ENDPOINT")]
    stt_endpoint: Option<String>,

    #[arg(long, env = "AI_VISION_ENDPOINT")]
    vision_endpoint: Option<String>,

    /// Queue message storage backend.
    #[arg(long, value_enum, default_value_t = QueueStorage::Memory, env = "PULSE_QUEUE_BACKEND")]
    queue_storage: QueueStorage,

    /// Directory for WAL queue data (only used with --queue-storage wal).
    #[arg(long, default_value = "./data/queues", env = "PULSE_QUEUE_WAL_DIR")]
    queue_data_dir: String,

    /// PostgreSQL connection URL (only used with --queue-storage postgres).
    #[arg(long, env = "PULSE_QUEUE_POSTGRES_URL")]
    database_url: Option<String>,

    /// Redis connection URL (only used with --queue-storage redis).
    #[arg(long, env = "PULSE_QUEUE_REDIS_URL")]
    redis_url: Option<String>,

    /// Hex-encoded 32-byte encryption key for queue data at rest.
    /// When set, all message payloads are encrypted with ChaCha20-Poly1305.
    #[arg(long, env = "PULSE_QUEUE_KEY")]
    queue_encryption_key: Option<String>,

    /// Path to a file containing the raw 32-byte encryption key.
    #[arg(long)]
    queue_encryption_key_file: Option<String>,
}

impl From<Args> for ServeArgs {
    fn from(a: Args) -> Self {
        Self {
            mode: a.mode,
            bind: a.bind,
            public_endpoint: a.public_endpoint,
            region: a.region,
            lat: a.lat,
            lon: a.lon,
            control_url: a.control_url,
            capacity: a.capacity,
            stt_endpoint: a.stt_endpoint,
            vision_endpoint: a.vision_endpoint,
            queue_storage: a.queue_storage,
            queue_data_dir: a.queue_data_dir,
            database_url: a.database_url,
            redis_url: a.redis_url,
            queue_encryption_key: a.queue_encryption_key,
            queue_encryption_key_file: a.queue_encryption_key_file,
            auth: pulse_relay::auth::AuthConfig::default(),
        }
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Args::parse();
    pulse_relay::run(args.into()).await
}
