use crate::features::config::{device_store::DeviceStore, state::DeviceState};
use crate::tools::{
session::DeviceSession,
types::DeviceIdentifier,
};
use anyhow::{Context, Result};
use chrono::Utc;
use std::path::PathBuf;
pub async fn execute() -> Result<()> {
let current_host = DeviceState::get_current()?;
let device_id = DeviceIdentifier::Host(current_host);
let device = DeviceStore::find(&device_id)?;
let mut session = DeviceSession::connect(&device)
.context("Failed to connect to device")?;
let timestamp = Utc::now().format("%Y%m%d_%H%M%S");
let remote_filename = format!("/home/defaultuser/Pictures/Screenshots/audb_screenshot_{}.png", timestamp);
let remote_path = PathBuf::from(&remote_filename);
let dbus_command = format!(
"dbus-send --session --print-reply \
--dest=org.nemomobile.lipstick \
/org/nemomobile/lipstick/screenshot \
org.nemomobile.lipstick.saveScreenshot \
string:\"{}\"",
remote_filename
);
session.exec_as_root(&dbus_command)
.context("Screenshot requires root access. Set root password using: audb device add")?;
let base64_data = session.read_file_base64(&remote_path)
.context("Failed to read screenshot file")?;
print!("{}", base64_data);
let cleanup_cmd = format!("rm -f {}", remote_filename);
let _ = session.exec_as_root(&cleanup_cmd);
Ok(())
}