//! Handler for `mcptest validate`.

use anyhow::Result;

use crate::cli::args::validate::{ValidateArgs, ValidateFormatCli};
use crate::cli::default_config::resolve_default_config;
use crate::cli::exit_codes::EXIT_INVALID_INVOCATION;
use crate::cli::GlobalArgs;
use crate::commands;
use crate::commands::validate::ValidateFormat;

/// Handle `mcptest validate` by delegating to the loader. Converts the
/// clap-level format enum to the command-level one so `commands` stays free
/// of CLI-parsing types.
pub(crate) fn validate_command(global: &GlobalArgs, args: &ValidateArgs) -> Result<i32> {
    let cwd = std::env::current_dir()?;
    let path = match global.config.clone() {
        Some(path) => path,
        // WOR-1350: the bare default resolves `mcptest.yaml` then
        // `mcptest.yml`, exactly like `mcptest run`, so the two commands
        // can never disagree about which file they checked.
        None => match resolve_default_config(&cwd) {
            Ok(resolved) => resolved.into_path(),
            Err(ambiguous) => {
                eprintln!("mcptest validate: {}", ambiguous.message());
                return Ok(EXIT_INVALID_INVOCATION);
            }
        },
    };
    let format = match args.format {
        ValidateFormatCli::Human => ValidateFormat::Human,
        ValidateFormatCli::Json => ValidateFormat::Json,
    };
    commands::run_validate(&path, format)
}
