mod agent;

use agent::{collect_profile, AgentRuntime, MachineProfile};
use tauri_plugin_store::Builder as StorePluginBuilder;

#[tauri::command]
fn collect_machine_profile() -> Result<MachineProfile, String> {
    collect_profile().map_err(|error| error.to_string())
}

#[tauri::command]
fn start_machine_agent(
    state: tauri::State<AgentRuntime>,
    base_url: String,
    token: String,
    status: Option<String>,
    interval_seconds: Option<u64>,
) -> Result<(), String> {
    state
        .start_heartbeat(base_url, token, status, interval_seconds)
        .map_err(|error| error.to_string())
}

#[tauri::command]
fn stop_machine_agent(state: tauri::State<AgentRuntime>) -> Result<(), String> {
    state.stop();
    Ok(())
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .manage(AgentRuntime::new())
        .plugin(tauri_plugin_opener::init())
        .plugin(StorePluginBuilder::default().build())
        .invoke_handler(tauri::generate_handler![
            collect_machine_profile,
            start_machine_agent,
            stop_machine_agent
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
