use tauri::{WebviewUrl, WebviewWindowBuilder};

pub mod shims;

pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_http::init())
        .plugin(tauri_plugin_opener::init())
        .manage(cambium_core::workspace::HashCache::default())
        .setup(|app| {
            // `mut` is used only by the macOS and Windows blocks below, so on
            // Linux it is genuinely unused — without this, every Linux build
            // from source opens with an unused_mut warning. Scoped to Linux
            // rather than a blanket allow, so a future macOS/Windows change
            // that stops reassigning still gets flagged.
            #[cfg_attr(
                not(any(target_os = "macos", target_os = "windows")),
                allow(unused_mut)
            )]
            let mut builder =
                WebviewWindowBuilder::new(app, "main", WebviewUrl::default())
                    .title("Cambium")
                    .inner_size(1280.0, 800.0)
                    .min_inner_size(940.0, 600.0)
                    .background_color(tauri::window::Color(0x14, 0x14, 0x14, 0xff));

            // macOS: hidden-inset titlebar equivalent — content under an overlay
            // titlebar, traffic lights centered in the 32px in-page menu bar
            // (--menu-bar-height in src/styles/variables.css).
            //
            // Y math: the visible traffic-light dots are 12px tall, so naive
            // centering in a 32px bar gives (32 - 12) / 2 = 10. In practice
            // the y-inset positions the top of the button *frame*, which
            // extends a few px above the visible glyph, so y=10 renders the
            // dots visibly high in the bar. y=15 places the visible dots at
            // the optical center of the 32px bar (tuned against the running app).
            #[cfg(target_os = "macos")]
            {
                use tauri::{LogicalPosition, TitleBarStyle};
                builder = builder
                    .title_bar_style(TitleBarStyle::Overlay)
                    .hidden_title(true)
                    .traffic_light_position(LogicalPosition::new(12.0, 15.0));
            }

            // Windows: drop the native Win11 titlebar so the in-page 32px menu
            // bar becomes the whole-window titlebar (custom min/max/close controls
            // live in MenuBar.jsx). The window keeps resizable(true) + snap.
            #[cfg(target_os = "windows")]
            {
                builder = builder.decorations(false);
            }

            builder.build()?;
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![
            shims::get_device_name,
            shims::read_workspace_tree,
            shims::set_workspace_writable,
            shims::delete_workspace_files,
            shims::import_workspace_paths,
            shims::create_workspace_folder,
            shims::pull_workspace_files,
            shims::upload_workspace_files,
            shims::load_client_state,
            shims::save_client_state,
            shims::pinned_fetch,
            shims::fetch_server_certificate,
            shims::save_secret,
            shims::load_secret,
            shims::delete_secret,
            shims::get_device_identity,
            shims::pins_all,
            shims::pins_set,
            shims::pins_remove,
            shims::workspaces_load,
            shims::workspaces_save,
            shims::run_console_command,
            shims::run_command_structured,
            shims::compute_status,
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
