use crate::config::AppConfig;
use crate::error::Result;
use crate::output::{is_json_output, output_success};
use clap::Args;
use colored::*;

#[derive(Args)]
pub struct VersionCommand {
    /// Output in JSON format
    #[arg(long)]
    json: bool,
}

impl VersionCommand {
    pub async fn execute(&self, config: &AppConfig) -> Result<()> {
        let version = env!("CARGO_PKG_VERSION");
        let name = env!("CARGO_PKG_NAME");
        let description = env!("CARGO_PKG_DESCRIPTION");
        let env = config.get_environment();

        if is_json_output() || self.json {
            output_success(serde_json::json!({
                "name": name,
                "version": version,
                "description": description,
                "environment": env.as_str(),
            }))?;
        } else {
            println!("\n{}\n", "Optima Ops CLI (Rust Edition)".cyan().bold());
            println!("  版本: {}", version.yellow());
            println!("  环境: {}", env.as_str().yellow());
            println!();
        }

        Ok(())
    }
}
