mod crypto;
mod db;
mod domains;
mod error;
mod helpers;
mod shared;

use db::connection::create_db_manager;
use shared::session::create_session;
use std::path::PathBuf;

pub fn run() {
    let app_data_dir = dirs_next::data_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join("com.eritors.scribe.desktop");

    let db_manager = create_db_manager(app_data_dir);
    let session = create_session();

    tauri::Builder::default()
        .plugin(tauri_plugin_shell::init())
        .manage(db_manager)
        .manage(session)
        .invoke_handler(tauri::generate_handler![
            // ─── User ──────────────────────────────────────
            domains::user::commands::init_user,
            domains::user::commands::db_initialize,
            domains::user::commands::get_token,
            domains::user::commands::set_token,
            domains::user::commands::remove_token,
            domains::user::commands::get_user_encryption_key,
            domains::user::commands::get_platform,
            domains::user::commands::get_user_info,
            domains::user::commands::sync_user,
            domains::user::commands::update_user_info,
            domains::user::commands::dev_reset_all,
            // ─── Offline ───────────────────────────────────
            domains::offline::commands::offline_pin_set,
            domains::offline::commands::offline_pin_verify,
            domains::offline::commands::offline_mode_get,
            domains::offline::commands::offline_mode_set,
            domains::offline::commands::offline_sync_check,
            // ─── Book (includes story, guideline, export, sync) ─
            domains::book::commands::get_books,
            domains::book::commands::get_book,
            domains::book::commands::get_book_basic_information,
            domains::book::commands::create_book,
            domains::book::commands::update_book_basic_info,
            domains::book::commands::delete_book,
            domains::book::commands::update_book_tool_setting,
            domains::book::commands::get_book_story,
            domains::book::commands::update_book_story,
            domains::book::commands::add_incident,
            domains::book::commands::remove_incident,
            domains::book::commands::add_plot_point,
            domains::book::commands::remove_plot_point,
            domains::book::commands::add_issue,
            domains::book::commands::remove_issue,
            domains::book::commands::get_worlds,
            domains::book::commands::add_world,
            domains::book::commands::add_world_element,
            domains::book::commands::remove_world_element,
            domains::book::commands::update_world,
            domains::book::commands::get_guideline,
            domains::book::commands::update_guideline,
            domains::book::commands::get_ai_guideline,
            domains::book::commands::update_ai_guideline,
            domains::book::commands::get_book_export_info,
            domains::book::commands::export_book,
            domains::book::commands::get_synced_books,
            domains::book::commands::upload_book_to_server,
            domains::book::commands::sync_save_book,
            domains::book::commands::sync_book_to_client,
            domains::book::commands::sync_book_to_server,
            // ─── Chapter ───────────────────────────────────
            domains::chapter::commands::get_chapters,
            domains::chapter::commands::get_whole_chapter,
            domains::chapter::commands::get_chapter_story,
            domains::chapter::commands::get_companion_content,
            domains::chapter::commands::get_chapter_content,
            domains::chapter::commands::save_chapter_content,
            domains::chapter::commands::get_last_chapter,
            domains::chapter::commands::add_chapter,
            domains::chapter::commands::remove_chapter,
            domains::chapter::commands::update_chapter,
            domains::chapter::commands::add_chapter_information,
            domains::chapter::commands::remove_chapter_information,
            domains::chapter::commands::get_book_tags,
            // ─── Character ─────────────────────────────────
            domains::character::commands::get_character_list,
            domains::character::commands::get_character_attributes,
            domains::character::commands::create_character,
            domains::character::commands::add_character_attribute,
            domains::character::commands::delete_character_attribute,
            domains::character::commands::update_character,
            domains::character::commands::delete_character,
            // ─── Location ──────────────────────────────────
            domains::location::commands::get_all_locations,
            domains::location::commands::add_location_section,
            domains::location::commands::add_location_element,
            domains::location::commands::add_location_sub_element,
            domains::location::commands::update_locations,
            domains::location::commands::update_location_section_with_series_link,
            domains::location::commands::delete_location_section,
            domains::location::commands::delete_location_element,
            domains::location::commands::delete_location_sub_element,
            // ─── Spell ─────────────────────────────────────
            domains::spell::commands::get_spell_list,
            domains::spell::commands::get_spell_tags,
            domains::spell::commands::get_spell_detail,
            domains::spell::commands::create_spell,
            domains::spell::commands::update_spell,
            domains::spell::commands::delete_spell,
            domains::spell::commands::create_spell_tag,
            domains::spell::commands::update_spell_tag,
            domains::spell::commands::delete_spell_tag,
            // ─── Series ────────────────────────────────────
            domains::series::commands::get_series_list,
            domains::series::commands::get_series_detail,
            domains::series::commands::create_series,
            domains::series::commands::update_series,
            domains::series::commands::delete_series,
            domains::series::commands::get_series_books,
            domains::series::commands::add_book_to_series,
            domains::series::commands::remove_book_from_series,
            domains::series::commands::reorder_series_books,
            domains::series::commands::get_series_for_book,
            // ─── Series Character ──────────────────────────
            domains::series_character::commands::get_series_character_list,
            domains::series_character::commands::get_series_character_attributes,
            domains::series_character::commands::add_series_character,
            domains::series_character::commands::update_series_character,
            domains::series_character::commands::delete_series_character,
            domains::series_character::commands::add_series_character_attribute,
            domains::series_character::commands::delete_series_character_attribute,
            // ─── Series Location ───────────────────────────
            domains::series_location::commands::get_series_location_list,
            domains::series_location::commands::add_series_location_section,
            domains::series_location::commands::add_series_location_element,
            domains::series_location::commands::add_series_location_sub_element,
            domains::series_location::commands::delete_series_location,
            domains::series_location::commands::delete_series_location_element,
            domains::series_location::commands::delete_series_location_sub_element,
            // ─── Series World ──────────────────────────────
            domains::series_world::commands::get_series_world_list,
            domains::series_world::commands::add_series_world,
            domains::series_world::commands::update_series_world,
            domains::series_world::commands::add_series_world_element,
            domains::series_world::commands::delete_series_world_element,
            // ─── Series Spell ──────────────────────────────
            domains::series_spell::commands::get_series_spell_list,
            domains::series_spell::commands::get_series_spell_detail,
            domains::series_spell::commands::add_series_spell,
            domains::series_spell::commands::update_series_spell,
            domains::series_spell::commands::delete_series_spell,
            domains::series_spell::commands::add_series_spell_tag,
            domains::series_spell::commands::update_series_spell_tag,
            domains::series_spell::commands::delete_series_spell_tag,
            // ─── Series Sync ───────────────────────────────
            domains::series_sync::commands::upload_series_to_server,
            domains::series_sync::commands::sync_save_series,
            domains::series_sync::commands::sync_series_to_client,
            domains::series_sync::commands::sync_series_to_server,
            domains::series_sync::commands::series_sync_upload,
            // ─── Sync ──────────────────────────────────────
            domains::sync::commands::get_synced_series,
            // ─── Tombstone ─────────────────────────────────
            domains::tombstone::commands::get_tombstones_since,
            domains::tombstone::commands::apply_book_tombstones,
            domains::tombstone::commands::apply_series_tombstones,
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
