//! Dirigent Core Server
//!
//! Main server application that orchestrates ACP agents

use axum::{response::Json, routing::get, Router};
use dirigent_acp_api::create_api_router;
use dirigent_core::CoreConfig;
use std::net::SocketAddr;
use tower_http::cors::CorsLayer;
use tracing_subscriber;

const DEFAULT_PORT: u16 = 3000;

#[tokio::main]
async fn main() {
    // Initialize tracing
    tracing_subscriber::fmt::init();

    let _config = CoreConfig::default();

    tracing::info!("Starting Dirigent Core server on port {}", DEFAULT_PORT);

    // Create the main application router
    let app = Router::new()
        .route("/", get(root_handler))
        .route("/info", get(info_handler))
        .nest("/api", create_api_router())
        .layer(CorsLayer::permissive());

    // Start the server
    let addr = SocketAddr::from(([127, 0, 0, 1], DEFAULT_PORT));
    tracing::info!("Listening on {}", addr);

    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

async fn root_handler() -> &'static str {
    "Dirigent Core - ACP Agent Orchestrator\n\nEndpoints:\n  GET /info - Server info\n  GET /api - ACP API\n  GET /api/health - Health check"
}

async fn info_handler() -> Json<serde_json::Value> {
    Json(serde_json::json!({
        "name": "dirigent_core",
        "version": env!("CARGO_PKG_VERSION"),
        "description": "Orchestrates agentic clients with ACP",
    }))
}
