use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _};
use std::fs;
use std::path::PathBuf;
use tauri::{AppHandle, Emitter};

pub fn process_png_file(path: &PathBuf, app: AppHandle) -> Result<(), String> {
    println!("Processing PNG file: {}", path.display());

    // Read the file
    let contents = fs::read(path).map_err(|e| format!("Failed to read file: {}", e))?;

    // Convert to base64
    let base64_string = BASE64.encode(&contents);
    println!("Generated base64 string of length: {}", base64_string.len());

    // Emit the base64 to frontend
    app.emit("png-processed", base64_string)
        .map_err(|e| format!("Failed to emit event: {}", e))?;

    println!("Successfully processed file: {}", path.display());
    Ok(())
}
