//! Handler for `mcptest generate <subcommand>`.

use anyhow::{Context, Result};

use crate::cli::args::{GenerateArgs, GenerateCommand};
use crate::commands;

/// Handle `mcptest generate <subcommand>`.
pub(crate) fn generate_command(args: GenerateArgs) -> Result<i32> {
    match args.command {
        GenerateCommand::Stubs(s) => {
            let bearer_token = s
                .bearer_token_env
                .as_ref()
                .and_then(|name| std::env::var(name).ok());
            // `--server-command` is one shell-quoted string, split with
            // `shell-words` like the global flag of the same name.
            let server_command = match &s.server_command {
                Some(raw) => shell_words::split(raw)
                    .with_context(|| format!("failed to parse --server-command: {raw}"))?,
                None => Vec::new(),
            };
            commands::run_generate_stubs(commands::GenerateStubsArgs {
                url: s.url,
                server_command,
                server_name: s.server_name,
                bearer_token,
                output_dir: s.output,
                overwrite: s.overwrite,
                stdout: s.stdout,
                check: s.check,
                include_output_schema_tests: !s.no_output_schema_tests,
                probe: s.probe,
                quiet: false,
            })
        }
        GenerateCommand::Suite(s) => commands::run_generate_suite(commands::GenerateSuiteArgs {
            from_config: s.from_config,
            server_name: s.server_name,
            models: s.models,
            include_edge: !s.no_edge,
            include_violation: !s.no_violation,
            include_output_schema_tests: !s.no_output_schema_tests,
            probe: s.probe,
            output: s.output,
            update: s.update,
            quiet: false,
        }),
        GenerateCommand::Conformance(s) => {
            commands::run_generate_conformance(commands::GenerateConformanceArgs {
                from_config: s.from_config,
                server_name: s.server_name,
                spec_version: s.spec_version,
                output: s.output,
                quiet: false,
            })
        }
        GenerateCommand::Redteam(s) => {
            commands::run_generate_redteam(commands::GenerateRedteamArgs {
                from_config: s.from_config,
                server_name: s.server_name,
                model: s.model,
                output: s.output,
                quiet: false,
            })
        }
        GenerateCommand::Eval(s) => commands::run_generate_eval(commands::GenerateEvalArgs {
            from_config: s.from_config,
            server_name: s.server_name,
            model: s.model,
            output: s.output,
            quiet: false,
        }),
    }
}
