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

use anyhow::Result;

use crate::cli::args::{ComplianceArgs, ComplianceCommand, ComplianceFormat, InvariantsFormat};
use crate::cli::GlobalArgs;
use crate::commands;

/// Handle `mcptest compliance <subcommand>`.
///
/// Translates the clap-derived args into the plain-data command inputs the
/// command handlers consume, then dispatches.
pub(crate) fn compliance_command(global: &GlobalArgs, args: ComplianceArgs) -> Result<i32> {
    match args.command {
        ComplianceCommand::Invariants(i) => {
            let format = match i.format {
                InvariantsFormat::Pretty => commands::InvariantsOutputFormat::Pretty,
                InvariantsFormat::Json => commands::InvariantsOutputFormat::Json,
            };
            commands::run_invariants(commands::InvariantsRunCliArgs {
                capture: i.capture,
                format,
            })
        }
        ComplianceCommand::Run(r) => {
            let format = match r.format {
                ComplianceFormat::Pretty => commands::ComplianceOutputFormat::Pretty,
                ComplianceFormat::Json => commands::ComplianceOutputFormat::Json,
                ComplianceFormat::Markdown => commands::ComplianceOutputFormat::Markdown,
                ComplianceFormat::Html => commands::ComplianceOutputFormat::Html,
            };
            let server_label = r
                .server_label
                .clone()
                .or_else(|| global.server_url.clone())
                .unwrap_or_else(|| "(unspecified)".into());
            let baseline = r.baseline.clone().or_else(|| r.expected_failures.clone());
            commands::run_compliance(commands::ComplianceRunCliArgs {
                format,
                results_from: r.results_from,
                from_suite: r.from_suite,
                server_label,
                registry: r.registry,
                capabilities: r.capabilities,
                baseline,
                update_baseline: r.update_baseline,
                yes: r.yes,
                save_scorecard: r.save_scorecard,
                official: r.official,
                official_cmd: r.official_cmd,
                server_url: global.server_url.clone(),
            })
        }
    }
}
