//! Basic K3s cluster setup example
//!
//! This example demonstrates how to set up a simple K3s cluster with one master and two worker nodes.
//!
//! Run with:
//! ```bash
//! cargo run --example basic_setup
//! ```

use lmrc_k3s::K3sManager;

#[tokio::main]
async fn main() -> Result<(), lmrc_k3s::K3sError> {
    // Initialize tracing for logs
    tracing_subscriber::fmt::init();

    // Create a K3s manager with default settings
    let manager = K3sManager::builder()
        .token("my-secure-cluster-token")
        .build();

    // Define your node IPs
    let master_ip = "192.168.1.10";
    let worker1_ip = "192.168.1.11";
    let worker2_ip = "192.168.1.12";

    println!("Setting up K3s cluster...");

    // Install K3s on the master node (force=false means it won't reinstall if already present)
    println!("Installing K3s on master node: {}", master_ip);
    manager.install_master(master_ip, None, false).await?;

    // Join worker nodes to the cluster
    println!("Joining worker node: {}", worker1_ip);
    manager.join_worker(worker1_ip, master_ip, None).await?;

    println!("Joining worker node: {}", worker2_ip);
    manager.join_worker(worker2_ip, master_ip, None).await?;

    // Download the kubeconfig file
    println!("Downloading kubeconfig...");
    manager.download_kubeconfig(master_ip, "kubeconfig").await?;

    // Get and display cluster state
    if let Some(state) = manager.get_cluster_state() {
        println!("\nCluster State:");
        println!("  Master: {}", state.master_ip);
        println!("  Workers: {}", state.workers.len());
        println!("  Ready workers: {}", state.ready_workers_count());
        println!("  Status: {:?}", state.status);
    }

    println!("\nCluster setup complete!");
    println!("You can now use kubectl with:");
    println!("  export KUBECONFIG=./kubeconfig");
    println!("  kubectl get nodes");

    Ok(())
}
