use anyhow::Result;
use tracing::info;

use crate::client::AdminClient;
use crate::output::{format_cluster_summary, format_json, OutputFormat};

/// Show cluster status overview
pub async fn cluster_status(client: &AdminClient, format: OutputFormat) -> Result<()> {
    info!("Fetching cluster status");
    let status = client.cluster_status().await?;

    match format {
        OutputFormat::Table => {
            println!("{}", format_cluster_summary(&status));
        }
        OutputFormat::Json => {
            println!("{}", format_json(&status)?);
        }
    }

    Ok(())
}

/// Quick health check (exit code 0 if healthy, 1 if unhealthy)
pub async fn cluster_health(client: &AdminClient, format: OutputFormat) -> Result<()> {
    info!("Checking cluster health");
    let health = client.health().await?;

    match format {
        OutputFormat::Table => {
            if health.healthy {
                println!("✓ Cluster is healthy");
                println!("  Reachable nodes: {}", health.reachable_nodes);
                println!("  Joined: {}", health.joined);
            } else {
                println!("✗ Cluster is unhealthy");
                println!("  Reachable nodes: {}", health.reachable_nodes);
                println!("  Joined: {}", health.joined);
            }
        }
        OutputFormat::Json => {
            println!("{}", format_json(&health)?);
        }
    }

    // Set exit code based on health
    if !health.healthy {
        std::process::exit(1);
    }

    Ok(())
}
