//! Tauri event helpers for the portfolio subsystem.

use serde::Serialize;
use tauri::{AppHandle, Emitter, Runtime};

#[derive(Debug)]
pub struct PortfolioEventEmitter<R: Runtime> {
    app_handle: AppHandle<R>,
}

// Manual Clone implementation that doesn't require R: Clone
impl<R: Runtime> Clone for PortfolioEventEmitter<R> {
    fn clone(&self) -> Self {
        Self {
            app_handle: self.app_handle.clone(),
        }
    }
}

impl<R: Runtime> PortfolioEventEmitter<R> {
    pub fn new(app_handle: &AppHandle<R>) -> Self {
        Self {
            app_handle: app_handle.clone(),
        }
    }

    /// Emitted when portfolio loading starts
    pub fn loading_started(&self) {
        let _ = self.app_handle.emit("portfolio:loading_started", ());
    }

    /// Emitted when basic portfolio structure is loaded (cached data)
    pub fn loaded<T: Serialize>(&self, payload: &T) {
        let _ = self.app_handle.emit("portfolio:loaded", payload);
    }

    /// Emitted when quote fetching begins
    #[allow(dead_code)]
    pub fn quotes_fetching(&self, symbol_count: usize) {
        let _ = self
            .app_handle
            .emit("portfolio:quotes_fetching", symbol_count);
    }

    /// Emitted with quote fetching progress
    #[allow(dead_code)]
    pub fn quotes_progress(&self, current: usize, total: usize) {
        let _ = self
            .app_handle
            .emit("portfolio:quotes_progress", (current, total));
    }

    /// Emitted when portfolio is fully loaded with fresh quotes
    pub fn ready<T: Serialize>(&self, payload: &T) {
        let _ = self.app_handle.emit("portfolio:ready", payload);
    }

    /// Emitted when an error occurs during loading
    #[allow(dead_code)]
    pub fn error(&self, error: &str) {
        let _ = self.app_handle.emit("portfolio:error", error);
    }
}
