//! RAG system command-line tool

use std::path::PathBuf;

use clap::Parser;
use provisioning_rag::config::RagConfig;

#[derive(Parser, Debug)]
#[command(name = "rag")]
#[command(about = "Retrieval-Augmented Generation system")]
struct Args {
    /// Configuration file path (highest priority)
    #[arg(short = 'c', long, env = "RAG_CONFIG")]
    config: Option<PathBuf>,

    /// Configuration directory (searches for rag.ncl|toml|json)
    #[arg(long, env = "PROVISIONING_CONFIG_DIR")]
    config_dir: Option<PathBuf>,

    /// Deployment mode (solo, multiuser, cicd, enterprise)
    #[arg(short = 'm', long, env = "RAG_MODE")]
    mode: Option<String>,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Parse CLI arguments FIRST (so --help works before any other processing)
    let _args = Args::parse();

    // Initialize centralized observability (logging, metrics, health checks)
    let _guard = observability::init_from_env("rag", env!("CARGO_PKG_VERSION"))?;

    // Check if rag is enabled in deployment-mode.ncl
    if let Ok(deployment) = platform_config::load_deployment_mode() {
        if let Ok(enabled) = deployment.is_service_enabled("rag") {
            if !enabled {
                tracing::warn!("⚠ RAG is DISABLED in deployment-mode.ncl");
                std::process::exit(1);
            }
            tracing::info!("✓ RAG is ENABLED in deployment-mode.ncl");
        }
    }

    // Try to load rag.ncl
    if let Ok(config) = platform_config::load_service_config_from_ncl("rag") {
        tracing::info!("✓ Loaded rag configuration from NCL");
        tracing::debug!("Config: {:?}", config);
    }

    // Load configuration
    let _config = RagConfig::default();

    println!("🚀 Provisioning RAG System");
    println!("Framework: Rig");
    println!("Vector Database: SurrealDB");

    Ok(())
}
