// Tauri Events Module
// This module demonstrates event emission from Rust to the frontend

use serde::{Deserialize, Serialize};
use tauri::{Emitter, EmitterExt};

/// Example event payload
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProgressEvent {
    pub current: u32,
    pub total: u32,
    pub message: String,
}

/// Emit a progress event
pub fn emit_progress(app: &tauri::AppHandle, current: u32, total: u32, message: &str) -> tauri::Result<()> {
    app.emit("progress", ProgressEvent {
        current,
        total,
        message: message.to_string(),
    })
}

/// Emit a data update event
pub fn emit_data_update(app: &tauri::AppHandle, data: &str) -> tauri::Result<()> {
    #[derive(Serialize)]
    struct DataUpdate {
        data: String,
        timestamp: u64,
    }

    app.emit("data-update", DataUpdate {
        data: data.to_string(),
        timestamp: std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs(),
    })
}

/// Emit an error event
pub fn emit_error(app: &tauri::AppHandle, error: &str) -> tauri::Result<()> {
    #[derive(Serialize)]
    struct ErrorEvent {
        error: String,
        timestamp: u64,
    }

    app.emit("error", ErrorEvent {
        error: error.to_string(),
        timestamp: std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs(),
    })
}

/// Emit a notification event
pub fn emit_notification(app: &tauri::AppHandle, title: &str, body: &str) -> tauri::Result<()> {
    #[derive(Serialize)]
    struct NotificationEvent {
        title: String,
        body: String,
    }

    app.emit("notification", NotificationEvent {
        title: title.to_string(),
        body: body.to_string(),
    })
}
