// Prevents an additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

extern crate core;

use log::{info, warn};
use mindwork_ai_studio::app_window::start_tauri;
use mindwork_ai_studio::runtime_certificate::{generate_runtime_certificate};
use mindwork_ai_studio::environment::is_dev;
use mindwork_ai_studio::log::init_logging;
use mindwork_ai_studio::metadata::MetaData;
use mindwork_ai_studio::runtime_api::start_runtime_api;
use mindwork_ai_studio::secret::init_secret_store;

// Keep `main` synchronous. Tauri owns the application's Tokio runtime, and Tauri itself as
// well as synchronous plugins may internally call `block_on` while they are initialized.
// In v26.7.3, `#[tokio::main]` caused the Linux single-instance plugin's synchronous D-Bus
// setup to enter `block_on` through zbus/tokio. Cargo feature unification made that path use
// Tokio, so startup panicked because a runtime was being started from inside another runtime.
//
// Run asynchronous background work with `tauri::async_runtime::spawn`. If startup must await
// asynchronous work, call `tauri::async_runtime::block_on` from this synchronous function
// before Tauri enters its event loop. If a real `#[tokio::main]` ever becomes unavoidable,
// first call `tauri::async_runtime::set(tokio::runtime::Handle::current())` before using any
// Tauri async function. Then audit every synchronously initialized Tauri plugin and transitive
// dependency for internal `block_on` calls. In particular, the Linux single-instance/D-Bus
// path must be made async, replaced, or moved to a non-conflicting backend. Such a runtime
// change requires explicit Linux startup tests; compiling successfully is not sufficient.
fn main() {
    let metadata = MetaData::init_from_string(include_str!("../../metadata.txt"));
    let tauri_context = tauri::generate_context!();
    let bundle_identifier = tauri_context.config().identifier.clone();

    init_logging(&bundle_identifier);
    info!("Starting MindWork AI Studio:");
    
    let working_directory = std::env::current_dir().unwrap();
    info!(".. The working directory is: '{working_directory:?}'");
    
    info!(".. Version: v{app_version} (commit {hash}, build {build_num}, {architecture})",
        app_version = metadata.app_version,
        hash = metadata.app_commit_hash,
        build_num = metadata.build_number,
        architecture = metadata.architecture);
    info!(".. Build time: {build_time}", build_time = metadata.build_time);
    info!(".. .NET SDK: v{sdk_version}", sdk_version = metadata.dotnet_sdk_version);
    info!(".. .NET: v{dotnet_version}", dotnet_version = metadata.dotnet_version);
    info!(".. Rust: v{rust_version}", rust_version = metadata.rust_version);
    info!(".. MudBlazor: v{mud_blazor_version}", mud_blazor_version = metadata.mud_blazor_version);
    info!(".. Tauri: v{tauri_version}", tauri_version = metadata.tauri_version);
    info!(".. PDFium: v{pdfium_version}", pdfium_version = metadata.pdfium_version);
    info!(".. Vector store: v{vector_store_version}", vector_store_version = metadata.vector_store_version);

    if is_dev() {
        warn!("Running in development mode.");
    } else {
        info!("Running in production mode.");
    }

    init_secret_store();
    generate_runtime_certificate();
    start_runtime_api();
    
    start_tauri(tauri_context);
}