//! Makima Audio API Server binary.
//!
//! This server provides WebSocket-based speech-to-text streaming with optional persistence.

use std::sync::Arc;

use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

use makima::server::{run_server, state::AppState};

/// Default model paths (can be overridden via environment variables).
const DEFAULT_PARAKEET_MODEL_DIR: &str = "models/parakeet-tdt-0.6b-v3";
const DEFAULT_PARAKEET_EOU_DIR: &str = "models/realtime_eou_120m-v1-onnx";
const DEFAULT_SORTFORMER_MODEL_PATH: &str = "models/diarization/diar_streaming_sortformer_4spk-v2.1.onnx";

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

    tracing::info!("Starting Makima Listening API Server");

    // Read configuration from environment
    let port = std::env::var("PORT").unwrap_or_else(|_| "8080".to_string());
    let parakeet_dir = std::env::var("PARAKEET_MODEL_DIR")
        .unwrap_or_else(|_| DEFAULT_PARAKEET_MODEL_DIR.to_string());
    let parakeet_eou_dir = std::env::var("PARAKEET_EOU_DIR")
        .unwrap_or_else(|_| DEFAULT_PARAKEET_EOU_DIR.to_string());
    let sortformer_path = std::env::var("SORTFORMER_MODEL_PATH")
        .unwrap_or_else(|_| DEFAULT_SORTFORMER_MODEL_PATH.to_string());

    tracing::info!(
        parakeet = %parakeet_dir,
        eou = %parakeet_eou_dir,
        sortformer = %sortformer_path,
        "Loading ML models..."
    );

    // Load ML models
    let mut app_state = AppState::new(&parakeet_dir, &parakeet_eou_dir, &sortformer_path)
        .map_err(|e| anyhow::anyhow!("Failed to load models: {}", e))?;

    tracing::info!("Models loaded successfully");

    // Initialize database (optional - server works without it)
    if let Ok(database_url) = std::env::var("POSTGRES_CONNECTION_URI") {
        tracing::info!("Connecting to database...");
        match makima::db::create_pool(&database_url).await {
            Ok(pool) => {
                tracing::info!("Database connected successfully");
                app_state = app_state.with_db_pool(pool);
            }
            Err(e) => {
                tracing::warn!("Failed to connect to database: {}. Running without persistence.", e);
            }
        }
    } else {
        tracing::info!("POSTGRES_CONNECTION_URI not set. Running without persistence.");
    }

    let state = Arc::new(app_state);

    // Run the server
    let addr = format!("0.0.0.0:{}", port);
    run_server(state, &addr).await
}
