//! Handler for `mcptest discover`.

use anyhow::Result;

use crate::cli::args::DiscoverArgs;
use crate::commands;

/// Handle `mcptest discover` by routing to the well-known fetch (when
/// `--url` is set) or the streamable HTTP probe + scaffolder.
pub(crate) fn discover_command(args: &DiscoverArgs) -> Result<i32> {
    if let Some(base) = &args.url {
        if args.server.is_some() {
            eprintln!("mcptest discover: pass either <server> or --url, not both");
            return Ok(2);
        }
        return commands::run_discover_url(base, args.write.as_deref());
    }
    let Some(server) = args.server.as_deref() else {
        eprintln!("mcptest discover: missing <server> argument (or pass --url <BASE>)");
        return Ok(2);
    };
    let bearer = args
        .bearer_token_env
        .as_ref()
        .and_then(|name| std::env::var(name).ok());
    commands::run_discover(server, &args.output, bearer, args.graph)
}
