mod cnc;
mod grbl;
mod serial;

use cnc::GrblServer;
use rmcp::ServiceExt;
use serial::SerialConnection;
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Logs must go to stderr — MCP uses stdout for JSON-RPC
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::from_default_env()
                .add_directive(tracing::Level::DEBUG.into()),
        )
        .with_writer(std::io::stderr)
        .with_ansi(false)
        .init();

    tracing::info!("Starting grbl-mcp server");

    let serial = Arc::new(SerialConnection::new("/dev/ttyUSB0", 115200));
    let server = GrblServer::new(serial);

    let service = server
        .serve(rmcp::transport::io::stdio())
        .await
        .inspect_err(|e| tracing::error!("serving error: {e:?}"))?;

    service.waiting().await?;
    Ok(())
}
