mod llm;
mod rag;
mod generations;
mod commands;

use llm::AppState;
use std::sync::Mutex;
use tauri::Manager;

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_log::Builder::default().build())
        .plugin(tauri_plugin_store::Builder::default().build())
        .plugin(tauri_plugin_fs::init())
        .setup(|app| {
            let data_dir = app
                .path()
                .app_data_dir()
                .expect("app data dir")
                .join("dm-toolkit");
            let rag = rag::RagStore::open(&data_dir.join("lore"))
                .expect("open lore db");
            let gen = generations::GenerationStore::open(&data_dir)
                .expect("open generations db");
            app.manage(AppState {
                config: Mutex::new(llm::LlmConfig::default()),
                rag,
                gen,
            });
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![
            greet,
            commands::llm_commands::generate,
            commands::llm_commands::generate_stream,
            commands::llm_commands::get_llm_config,
            commands::llm_commands::set_llm_config,
            commands::llm_commands::test_connection,
            commands::image_commands::generate_image,
            commands::rag_commands::rag_add,
            commands::rag_commands::rag_search,
            commands::rag_commands::rag_list,
            commands::rag_commands::rag_clear,
            commands::generation_commands::generation_add,
            commands::generation_commands::generation_list,
            commands::generation_commands::generation_get,
            commands::generation_commands::generation_delete,
            commands::generation_commands::generation_counts,
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! Welcome to DM-Pal ⚔", name)
}