//! MCP-Relational-Data — Calculator Suite v2.1
//!
//! Originally written in Visual Basic 6 (1999). Modern port to Rust.
//!
//! # Usage
//!
//! ```text
//! mcp-relational-data              GUI mode (default, requires --features gui)
//! mcp-relational-data --daemon     Daemon mode — stdin/stdout JSON API
//! mcp-relational-data --help       Show usage info
//! ```
//!
//! # Feature flags
//!
//! | Flag     | Enables        | Effect                                    |
//! |----------|----------------|-------------------------------------------|
//! | `gui`    | iced 0.12      | Desktop GUI window                        |
//! | `daemon` | serde+json     | Stdin/stdout JSON API (MCP-friendly)      |
//! |          |                |                                           |
//! | default  | both           | `cargo run` builds everything              |
//!
//! # Build a slim daemon-only binary
//!
//! ```text
//! mcp-relational-data --no-default-features --features daemon -- --daemon
//! ```

mod calc;

#[cfg(feature = "daemon")]
mod daemon;

#[cfg(feature = "gui")]
mod gui;

fn main() {
    let args: Vec<String> = std::env::args().collect();

    // ── daemon flag (takes priority) ────────────────────────────────
    #[cfg(feature = "daemon")]
    if args.iter().any(|a| a == "--daemon") {
        daemon::run();
        return;
    }

    // ── help / version ─────────────────────────────────────────────
    if args.iter().any(|a| a == "--help" || a == "-h") {
        print_usage();
        return;
    }

    if args.iter().any(|a| a == "--version" || a == "-v") {
        println!("relational-data v{}", env!("CARGO_PKG_VERSION"));
        return;
    }

    // ── GUI (default when no flag or --gui) ────────────────────────
    #[cfg(feature = "gui")]
    {
        let _ = gui::run();
        return;
    }

    // ── fallback: daemon compiled but no flag → show help ──────────
    #[cfg(all(feature = "daemon", not(feature = "gui")))]
    {
        eprintln!("No GUI support compiled. Running in daemon mode.");
        eprintln!("Usage: relational-data --daemon");
        eprintln!();
        daemon::run();
        return;
    }

    // ── nothing compiled ────────────────────────────────────────────
    #[cfg(not(any(feature = "gui", feature = "daemon")))]
    {
        eprintln!("Error: no features enabled.");
        eprintln!("Build with:  cargo run --features gui");
        eprintln!("     or:    cargo run --features daemon -- --daemon");
        std::process::exit(1);
    }
}

fn print_usage() {
    println!("MCP-Relational-Data v{} — Calculator Suite", env!("CARGO_PKG_VERSION"));
    println!();
    println!("USAGE:");
    println!("  mcp-relational-data              GUI mode (default)");
    println!("  mcp-relational-data --daemon     Daemon mode (stdin/stdout JSON)");
    println!("  mcp-relational-data --help       Show this help");
    println!("  mcp-relational-data --version    Show version");
    println!();
    println!("DAEMON PROTOCOL (one JSON object per line):");
    println!("  {{\"tool\":\"list\"}}                  List available tools");
    println!("  {{\"tool\":\"help\",\"name\":\"ohm\"}}     Tool parameter docs");
    println!("  {{\"tool\":\"ohm\",\"current\":2,\"resistance\":4}}  Calculate");
    println!();
    println!("FEATURE FLAGS:");
    println!("  gui    — iced desktop GUI (default)");
    println!("  daemon — stdin/stdout JSON API (default)");
    println!();
    println!("SLIM DAEMON-ONLY BUILD:");
    println!("  mcp-relational-data --no-default-features --features daemon -- --daemon");
}