#![allow(dead_code, unused_imports, unused_variables, unused_assignments)]
#![allow(clippy::excessive_nesting)]

use leptos::prelude::*;
use wasm_bindgen::prelude::*;

mod api;
mod app;
mod auth;
mod components;
mod config;
mod hooks;
mod pages;
mod services;
mod store;
mod types;
mod utils;

use app::App;

// Main entry point - called automatically by wasm_bindgen
#[wasm_bindgen(start)]
pub fn main() {
    // Initialize panic hook for better error messages in development
    console_error_panic_hook::set_once();

    // Initialize tracing for logging
    tracing_wasm::set_as_global_default();

    // Log application startup
    tracing::info!("Starting Control Center UI");

    // Also log to browser console
    web_sys::console::log_1(&"🚀 Control Center UI WASM loaded and main() called".into());

    // Mount app to body
    mount_to_body(|| {
        view! {
            <App/>
        }
    });

    web_sys::console::log_1(&"✅ Leptos app mounted to #leptos element".into());
}

// Export functions that can be called from JavaScript
#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = console)]
    fn log(s: &str);

    #[wasm_bindgen(js_namespace = localStorage)]
    fn getItem(key: &str) -> Option<String>;

    #[wasm_bindgen(js_namespace = localStorage)]
    fn setItem(key: &str, value: &str);
}

// Helper macro for console logging
#[allow(unused_macros)]
macro_rules! console_log {
    ($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
}

#[allow(unused_imports)]
pub(crate) use console_log;

// Utility functions for localStorage interaction
pub fn get_local_storage_item(key: &str) -> Option<String> {
    getItem(key)
}

pub fn set_local_storage_item(key: &str, value: &str) {
    setItem(key, value);
}

// Theme utilities
pub fn get_saved_theme() -> String {
    get_local_storage_item("theme").unwrap_or_else(|| "light".to_string())
}

pub fn save_theme(theme: &str) {
    set_local_storage_item("theme", theme);
}

// Performance monitoring
#[wasm_bindgen]
pub fn mark_performance(name: &str) {
    #[wasm_bindgen]
    extern "C" {
        #[wasm_bindgen(js_namespace = ["window", "performance"], js_name = mark)]
        fn performance_mark(name: &str);
    }

    performance_mark(name);
}
