use sshr::ssh::{AuthMethod, FileService, ReadFileRequest, RusshTool, SshConfig, WriteFileRequest};

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

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

    // List directory
    let entries = tool.list_dir(cfg.clone(), "/etc".to_string()).await?;
    for entry in entries.iter().take(10) {
        let kind = if entry.is_dir { "d" } else { "-" };
        println!("{} {:>8} {}", kind, entry.size, entry.name);
    }

    // Read file
    let content = tool
        .read_file(
            cfg.clone(),
            ReadFileRequest {
                path: "/etc/os-release".to_string(),
                limit: Some(256),
                ..Default::default()
            },
        )
        .await?;
    println!("\n--- /etc/os-release (first 256 bytes) ---");
    println!("{}", String::from_utf8_lossy(&content));

    // Write file
    tool.write_file(
        cfg,
        WriteFileRequest {
            path: "/tmp/sshr-test.txt".to_string(),
            content: b"hello from sshr\n".to_vec(),
            create_parent: true,
            overwrite: true,
        },
    )
    .await?;
    println!("\n✓ wrote /tmp/sshr-test.txt");

    Ok(())
}
