//! Handler for `mcptest propose` (WOR-1262). Lowers the clap struct into the
//! plain-data [`crate::commands::ProposeRunArgs`]: splits `--server-command`
//! with POSIX shell rules (the same convention as the global server
//! override) and resolves the bearer-token env var, so the engine stays free
//! of clap and process environment.

use anyhow::{Context, Result};

use crate::cli::args::ProposeArgs;
use crate::commands;

/// Dispatch for `mcptest propose`.
pub(crate) fn propose_command(args: ProposeArgs) -> Result<i32> {
    let server_command = match &args.server_command {
        Some(raw) => Some(
            shell_words::split(raw)
                .with_context(|| format!("failed to parse --server-command: {raw}"))?,
        ),
        None => None,
    };
    // An unset env var means an unauthenticated request, matching the other
    // `--bearer-token-env` surfaces.
    let bearer_token = args
        .bearer_token_env
        .as_ref()
        .and_then(|name| std::env::var(name).ok());
    commands::run_propose(commands::ProposeRunArgs {
        tool: args.tool,
        args_json: args.args,
        url: args.url,
        bearer_token,
        server_command,
        single_call: args.single_call,
        execute_destructive: args.execute_destructive,
    })
}
