//! QwenClaw Rig Service
//! 
//! A Rust-based AI agent service using Rig framework for:
//! - Multi-agent orchestration
//! - Dynamic tool calling
//! - RAG workflows
//! - Vector store integration

mod agent;
mod api;
mod tools;
mod vector_store;
mod config;

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

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize logging
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| "qwenclaw_rig=debug,info".into()),
        )
        .with(tracing_subscriber::fmt::layer())
        .init();

    // Load environment variables
    dotenvy::dotenv().ok();

    tracing::info!("🦀 Starting QwenClaw Rig Service...");

    // Initialize configuration
    let config = config::Config::from_env()?;
    
    // Initialize vector store
    let vector_store = vector_store::VectorStore::new(&config.database_path).await?;
    
    // Initialize tool registry
    let tool_registry = tools::ToolRegistry::new();
    
    // Create API server
    let app = api::create_app(config, vector_store, tool_registry);

    // Get host and port from config
    let addr = format!("{}:{}", config.host, config.port);
    let listener = tokio::net::TcpListener::bind(&addr).await?;
    
    tracing::info!("🚀 Rig service listening on http://{}", addr);
    tracing::info!("📚 API docs: http://{}/docs", addr);

    // Start server
    axum::serve(listener, app).await?;

    Ok(())
}
