use sshr::ssh::{
    AuthMethod, ConnectionPool, ExecRequest, ExecService, RusshTool, Session, SshConfig,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let args: Vec<String> = std::env::args().collect();
    if args.len() < 4 {
        eprintln!("usage: pool <host> <user> <password>");
        std::process::exit(1);
    }

    let cfg = SshConfig::new(
        &args[1],
        &args[2],
        AuthMethod::Password {
            password: args[3].clone(),
        },
    );

    // === Method 1: Session (manual connection management) ===
    println!("=== Session mode ===");
    let session = Session::connect(cfg.clone()).await?;
    for cmd in ["uname -a", "uptime", "df -h /"] {
        let result = session.exec(ExecRequest::new(cmd)).await?;
        println!("[{}] {}", cmd, result.stdout_string().trim());
    }
    session.close().await?;

    // === Method 2: ConnectionPool (automatic connection reuse) ===
    println!("\n=== Pool mode ===");
    let pool = ConnectionPool::default_pool();
    let tool = RusshTool::with_pool(pool);

    for i in 0..3 {
        let result = tool.exec(cfg.clone(), ExecRequest::new("hostname")).await?;
        println!("call {}: {}", i, result.stdout_string().trim());
    }

    Ok(())
}
