#![recursion_limit = "512"]

//! 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 mappings;
mod news;
mod portfolio;
mod research;
mod state;
mod terminal;
#[cfg(test)]
mod test_support;

use tauri::{Emitter, 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) })?;
            let news_service = state.news_service.clone();
            let research_service = state.research_service.clone();
            let app_handle = app.handle().clone();

            app.manage(state);
            news::scheduler::spawn_news_scheduler(news_service, move |result| {
                let _ = app_handle.emit("news_feed_updated", &result);
            });
            research::spawn_research_scheduler(research_service);
            Ok(())
        })
        .plugin(tauri_plugin_opener::init())
        .invoke_handler(tauri::generate_handler![
            commands::terminal::execute_terminal_command,
            commands::terminal::lookup_company,
            commands::terminal::start_chat_stream,
            commands::terminal::cancel_chat_stream,
            commands::terminal::resolve_agent_tool_approval,
            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::mappings::list_custom_mappings,
            commands::mappings::get_custom_mapping,
            commands::mappings::add_custom_mapping,
            commands::mappings::update_custom_mapping,
            commands::mappings::delete_custom_mapping,
            commands::mappings::toggle_custom_mapping,
            commands::mappings::import_mappings,
            commands::mappings::export_mappings,
            commands::mappings::validate_formula,
            commands::mappings::get_ticker_concepts,
            commands::news::query_news_feed,
            commands::news::refresh_news_feed,
            commands::news::get_news_feed_config,
            commands::news::save_news_feed_config,
            commands::news::reset_news_feed_config,
            commands::news::update_news_article_state,
            commands::research::create_research_workspace,
            commands::research::list_research_workspaces,
            commands::research::get_research_workspace,
            commands::research::capture_research_note,
            commands::research::preview_note_link,
            commands::research::update_research_note,
            commands::research::archive_research_note,
            commands::research::list_research_notes,
            commands::research::get_notes_by_ticker,
            commands::research::get_workspace_projection,
            commands::research::list_financial_models,
            commands::research::get_financial_model,
            commands::research::create_financial_model,
            commands::research::save_financial_model,
            commands::research::evaluate_financial_model,
            commands::research::list_model_data_bindings,
            commands::research::save_model_data_binding,
            commands::research::refresh_model_data_bindings,
            commands::research::link_note_to_model_cell,
            commands::research::unlink_note_from_model_cell,
            commands::research::update_model_cell_annotation,
            commands::research::list_model_cell_annotations,
            commands::research::export_financial_model_xlsx,
            commands::research::list_note_links,
            commands::research::list_workspace_ghost_notes,
            commands::research::review_ghost_note,
            commands::research::resolve_ghost_intervention,
            commands::research::promote_note_to_thesis,
            commands::research::retry_research_jobs,
            commands::research::list_research_jobs,
            commands::research::get_note_audit_trail,
            commands::research::export_research_bundle,
            commands::research::search_research_notes,
            commands::research::update_research_workspace,
            commands::research::archive_research_workspace,
            commands::research::list_research_files,
            commands::research::upload_research_files,
            commands::research::get_research_file_text_preview,
            commands::artifacts::list_artifacts,
            commands::artifacts::load_artifact,
            commands::artifacts::delete_artifact,
            commands::jobs::start_background_job,
            commands::jobs::get_background_job_status,
            commands::jobs::cancel_background_job
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
