use anyhow::Result;
use chrono::Utc;
use std::fs;
use tracing::info;

use crate::client::AdminClient;
use crate::types::ClusterDebugExport;

/// Export complete cluster state for debugging
pub async fn export_debug_state(client: &AdminClient, output_path: &str) -> Result<()> {
    info!("Exporting cluster state to: {}", output_path);

    // Gather all cluster information
    let health = client.health().await?;
    let cluster = client.cluster_status().await?;
    let shards = client.all_ranges().await?;

    let export = ClusterDebugExport {
        timestamp: Utc::now().to_rfc3339(),
        gateway_version: env!("CARGO_PKG_VERSION").to_string(),
        cluster,
        health,
        shards,
    };

    // Write to file
    let json = serde_json::to_string_pretty(&export)?;
    fs::write(output_path, json)?;

    println!("✓ Cluster state exported to: {}", output_path);
    println!("  Timestamp: {}", export.timestamp);
    println!("  Nodes:     {}", export.cluster.node_count);
    println!("  Shards:    {}", export.cluster.shard_count);

    Ok(())
}
