use lmrc_ssh::{AuthMethod, Error, SshClient};

fn main() {
    println!("SSH Manager - Error Handling Example\n");

    // Example 1: Invalid configuration
    println!("Example 1: Invalid Configuration");
    println!("─────────────────────────────────");
    match SshClient::new("", 22) {
        Ok(_) => println!("Unexpected success"),
        Err(Error::InvalidConfig(msg)) => println!("✓ Caught expected error: {}", msg),
        Err(e) => println!("✗ Unexpected error: {}", e),
    }
    println!();

    // Example 2: Connection failure
    println!("Example 2: Connection Failure");
    println!("─────────────────────────────────");
    match SshClient::new("nonexistent-host-12345.invalid", 22).and_then(|client| {
        client
            .with_auth(AuthMethod::Password {
                username: "user".to_string(),
                password: "pass".to_string(),
            })
            .connect()
    }) {
        Ok(_) => println!("Unexpected success"),
        Err(Error::ConnectionFailed { host, port, source }) => {
            println!("✓ Caught connection error:");
            println!("  Host: {}", host);
            println!("  Port: {}", port);
            println!("  Reason: {}", source);
        }
        Err(e) => println!("Error (might vary by system): {}", e),
    }
    println!();

    // Example 3: Not connected error
    println!("Example 3: Not Connected Error");
    println!("─────────────────────────────────");
    match SshClient::new("example.com", 22) {
        Ok(mut client) => {
            // Try to execute without connecting
            match client.execute("ls") {
                Ok(_) => println!("Unexpected success"),
                Err(Error::NotConnected) => println!("✓ Caught expected 'Not Connected' error"),
                Err(e) => println!("✗ Unexpected error: {}", e),
            }
        }
        Err(e) => println!("✗ Failed to create client: {}", e),
    }
    println!();

    // Example 4: Handling real connection (if environment is set)
    println!("Example 4: Real Connection Attempt");
    println!("─────────────────────────────────");

    if let (Ok(host), Ok(user), Ok(pass)) = (
        std::env::var("SSH_HOST"),
        std::env::var("SSH_USER"),
        std::env::var("SSH_PASSWORD"),
    ) {
        println!("Environment variables found, attempting connection...");

        match connect_and_execute(&host, &user, &pass) {
            Ok(output) => {
                println!("✓ Success!");
                println!("  Output: {}", output.trim());
            }
            Err(Error::AuthenticationFailed { username, reason }) => {
                println!("✗ Authentication failed:");
                println!("  Username: {}", username);
                println!("  Reason: {}", reason);
            }
            Err(Error::ConnectionFailed { host, port, source }) => {
                println!("✗ Connection failed:");
                println!("  Host: {}:{}", host, port);
                println!("  Reason: {}", source);
            }
            Err(e) => {
                println!("✗ Error: {}", e);
            }
        }
    } else {
        println!("Skipping (set SSH_HOST, SSH_USER, SSH_PASSWORD to test)");
    }

    println!("\n✓ Error handling examples completed!");
}

fn connect_and_execute(host: &str, user: &str, pass: &str) -> Result<String, Error> {
    let mut client = SshClient::new(host, 22)?
        .with_auth(AuthMethod::Password {
            username: user.to_string(),
            password: pass.to_string(),
        })
        .connect()?;

    let output = client.execute("echo 'Hello from SSH!'")?;
    Ok(output.stdout)
}
