use crate::features::config::{device_store::DeviceStore, state::DeviceState};
use crate::tools::macros::print_info;
use crate::tools::ssh::SshClient;
use crate::tools::types::DeviceIdentifier;
use crate::tools::validation::validate_rpm_exists;
use anyhow::{anyhow, Result};
use std::path::PathBuf;
pub async fn execute(rpm_path: &str) -> Result<()> {
let local_path = PathBuf::from(rpm_path);
validate_rpm_exists(&local_path)?;
let file_name = local_path
.file_name()
.ok_or_else(|| anyhow!("Invalid file name"))?
.to_string_lossy()
.to_string();
let current_host = DeviceState::get_current()?;
let device_id = DeviceIdentifier::Host(current_host);
let device = DeviceStore::find(&device_id)?;
print_info(format!("Installing {} on device {}", file_name, device.display_name()));
print_info(format!("Connecting to {}:{}...", device.host, device.port));
let mut session = SshClient::connect(&device.host, device.port, &device.auth_path())?;
let remote_path = PathBuf::from(format!("/home/defaultuser/Downloads/{}", file_name));
print_info(format!("Uploading {} to {}...", file_name, remote_path.display()));
SshClient::upload(&mut session, &local_path, &remote_path)?;
print_info("Installing package via APM...");
let install_command = format!(
"gdbus call --system --dest ru.omp.APM --object-path /ru/omp/APM --method ru.omp.APM.Install \"{}\" \"{{}}\"",
remote_path.display()
);
let output = SshClient::exec(&mut session, &install_command)?;
for line in &output {
if !line.is_empty() {
println!("{}", line);
}
}
print_info("Cleaning up temporary files...");
let cleanup_command = format!("rm -f {}", remote_path.display());
SshClient::exec(&mut session, &cleanup_command).ok();
println!("\n\x1b[1m\x1b[32msuccess\x1b[0m: Package installed successfully");
Ok(())
}