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

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

impl EnvCommand {
    pub async fn execute(&self, config: &AppConfig) -> Result<()> {
        let env = config.get_environment();
        let ec2_config = config.get_ec2_config(None);
        let env_info = env.get_env_info();

        let services: Vec<String> = config
            .get_all_services()
            .iter()
            .map(|s| s.name.clone())
            .collect();

        if is_json_output() || self.json {
            output_success(serde_json::json!({
                "environment": env.as_str(),
                "ec2_host": ec2_config.host,
                "rds_host": env_info.rds_host,
                "docker_network": env_info.docker_network,
                "services": services,
            }))?;
        } else {
            print_title("当前环境配置:");
            println!("  环境: {}", env.as_str().yellow());
            println!("  EC2 主机: {}", ec2_config.host);
            println!("  RDS 主机: {}", env_info.rds_host);
            println!("  Docker 网络: {}", env_info.docker_network);
            println!("  服务列表: {}", services.join(", "));
            println!();
        }

        Ok(())
    }
}
