//! Tauri backend for MosaicIQ.
//!
//! The backend is intentionally split into a small bridge layer (`commands`),
//! domain logic (`agent`), and shared runtime state (`state`) so UI-facing
//! commands do not accumulate business logic over time.

mod agent;
mod commands;
mod error;
mod state;
mod terminal;

use tauri::Manager;

/// Starts the Tauri application and registers the backend command surface.
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_store::Builder::new().build())
        .setup(|app| {
            let state = state::AppState::new(&app.handle())
                .map_err(|error| -> Box<dyn std::error::Error> { Box::new(error) })?;

            app.manage(state);
            Ok(())
        })
        .plugin(tauri_plugin_opener::init())
        .invoke_handler(tauri::generate_handler![
            commands::terminal::execute_terminal_command,
            commands::terminal::start_chat_stream,
            commands::settings::get_agent_config_status,
            commands::settings::save_agent_runtime_config,
            commands::settings::update_remote_api_key,
            commands::settings::clear_remote_api_key,
            commands::settings::list_local_models,
            commands::settings::check_local_provider_health
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
