//! `prov-mcp` — MCP JSON-RPC 2.0 server backed by the provisioning-core Registry.
//!
//! Reads `ORCHESTRATOR_URL`, `PROVISIONING_WORKSPACES_ROOT` from the environment.
//! Protocol transport: newline-delimited JSON on stdin/stdout.

use anyhow::Result;
use provisioning_core::{
    Environment,
    providers::hcloud::HcloudClient,
    sources::{NclCache, OrchestratorClient},
    Registry,
};
use provisioning_mcp_server::registry_server::McpServer;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::from_default_env()
                .add_directive(tracing::Level::WARN.into()),
        )
        .with_writer(std::io::stderr)
        .init();

    let orchestrator_url = std::env::var("ORCHESTRATOR_URL")
        .unwrap_or_else(|_| "http://localhost:9011".into());

    let cache  = Arc::new(NclCache::new());
    let orch   = Arc::new(OrchestratorClient::new(&orchestrator_url));
    let hcloud = Arc::new(HcloudClient::new());
    let registry = Registry::with_all_tools(cache, orch, hcloud, None)?;

    let mut env = Environment::default();
    env.orchestrator_url = orchestrator_url;
    if let Ok(root) = std::env::var("PROVISIONING_WORKSPACES_ROOT") {
        env.workspaces_root = root.into();
    }

    let server = McpServer::new(
        registry,
        env,
        env!("CARGO_PKG_NAME"),
        env!("CARGO_PKG_VERSION"),
    );

    server.run_stdio().await
}
