/// Logging utilities for VProxy Web application
/// 
/// This module provides convenient macros and functions for structured logging
/// that work on both server-side (SSR) and client-side (WASM).

// In production version we disable debug, trace and warn logs
#[cfg(debug_assertions)]
pub use log::{debug, error, info, trace, warn};

#[cfg(not(debug_assertions))]
pub use log::{error, info};

// Macros for conditional logging in production
#[cfg(not(debug_assertions))]
macro_rules! debug {
    ($($arg:tt)*) => {};
}

#[cfg(not(debug_assertions))]
macro_rules! trace {
    ($($arg:tt)*) => {};
}

#[cfg(not(debug_assertions))]
macro_rules! warn {
    ($($arg:tt)*) => {};
}

/// Log an info message with context
#[macro_export]
macro_rules! log_info {
    ($($arg:tt)*) => {
        log::info!("[VProxy] {}", format!($($arg)*))
    };
}

/// Log an error message with context
#[macro_export]
macro_rules! log_error {
    ($($arg:tt)*) => {
        log::error!("[VProxy] {}", format!($($arg)*))
    };
}

/// Log a warning message with context
/// In production version warnings are disabled
#[macro_export]
macro_rules! log_warn {
    ($($arg:tt)*) => {
        #[cfg(debug_assertions)]
        {
            log::warn!("[VProxy] {}", format!($($arg)*))
        }
    };
}

/// Log a debug message with context
/// In production version debug logs are disabled
#[macro_export]
macro_rules! log_debug {
    ($($arg:tt)*) => {
        #[cfg(debug_assertions)]
        {
            log::debug!("[VProxy] {}", format!($($arg)*))
        }
    };
}

/// Log user interactions for analytics
#[macro_export]
macro_rules! log_user_action {
    ($action:expr, $component:expr) => {
        log::info!("[VProxy][UserAction] {} in {}", $action, $component)
    };
    ($action:expr, $component:expr, $details:expr) => {
        log::info!("[VProxy][UserAction] {} in {} - {}", $action, $component, $details)
    };
}

/// Log component lifecycle events
#[macro_export]
macro_rules! log_component {
    (created, $component:expr) => {
        log::debug!("[VProxy][Component] {} created", $component)
    };
    (rendered, $component:expr) => {
        log::debug!("[VProxy][Component] {} rendered", $component)
    };
    (destroyed, $component:expr) => {
        log::debug!("[VProxy][Component] {} destroyed", $component)
    };
}

/// Log API requests and responses
#[macro_export]
macro_rules! log_api {
    (request, $endpoint:expr) => {
        log::info!("[VProxy][API] Request to {}", $endpoint)
    };
    (response, $endpoint:expr, $status:expr) => {
        log::info!("[VProxy][API] Response from {} - Status: {}", $endpoint, $status)
    };
    (error, $endpoint:expr, $error:expr) => {
        log::error!("[VProxy][API] Error from {} - {}", $endpoint, $error)
    };
}

/// Initialize logging based on environment
/// This is called automatically during app startup
pub fn init_logging() {
    #[cfg(feature = "ssr")]
    {
        log::info!("Server-side logging initialized");
    }
    
    #[cfg(feature = "hydrate")]
    {
        // In production version we show only critical messages
        #[cfg(debug_assertions)]
        {
            log::info!("Client-side logging initialized (DEBUG mode)");
        }
        #[cfg(not(debug_assertions))]
        {
            log::error!("Client-side logging initialized (PRODUCTION mode - warnings disabled)");
        }
    }
}

/// Get the current log level as a string
pub fn get_log_level() -> &'static str {
    if log::log_enabled!(log::Level::Trace) {
        "trace"
    } else if log::log_enabled!(log::Level::Debug) {
        "debug"
    } else if log::log_enabled!(log::Level::Info) {
        "info"
    } else if log::log_enabled!(log::Level::Warn) {
        "warn"
    } else {
        "error"
    }
}

/// Log performance metrics
#[macro_export]
macro_rules! log_perf {
    ($operation:expr, $duration:expr) => {
        log::info!("[VProxy][Performance] {} took {}ms", $operation, $duration)
    };
}

#[cfg(feature = "hydrate")]
/// Web-specific logging utilities for browser console
pub mod web {
    use web_sys::console;
    
    /// Log directly to browser console with custom styling
    pub fn console_info(message: &str) {
        console::log_1(&format!("🌐 VProxy: {}", message).into());
    }
    
    /// Log error directly to browser console
    pub fn console_error(message: &str) {
        console::error_1(&format!("❌ VProxy Error: {}", message).into());
    }
    
    /// Log warning directly to browser console
    pub fn console_warn(message: &str) {
        console::warn_1(&format!("⚠️ VProxy Warning: {}", message).into());
    }
}
