// Minimal CLI entry point for Nyash.
// Delegates to the library crate (`nyash_rust`) for all functionality.

use nyash_rust::cli::CliConfig;
use nyash_rust::config::env as env_config;
use nyash_rust::runner::NyashRunner;

/// Thin entry point - delegates to CLI parsing and runner execution
fn main() {
    // Optional: enable backtrace on stack overflow for deep debug runs.
    // Guarded by env to keep default behavior unchanged.
    if std::env::var("NYASH_DEBUG_STACK_OVERFLOW").ok().as_deref() == Some("1") {
        unsafe {
            let _ = backtrace_on_stack_overflow::enable();
        }
    }

    // hv1 direct (primary): earliest possible check before any bootstrap/log init
    // If NYASH_VERIFY_JSON is present and route is requested, execute and exit.
    // This avoids plugin host/registry initialization and keeps output minimal.
    let has_json = std::env::var("NYASH_VERIFY_JSON").is_ok();
    let route = nyash_rust::config::env::verify_primary_is_hakovm();
    // Force flag may allow hv1-inline without route
    let force_hv1_flag = nyash_rust::config::env::env_bool("HAKO_VERIFY_V1_FORCE_HAKOVM");
    if has_json && (route || force_hv1_flag) {
        let json = std::env::var("NYASH_VERIFY_JSON").unwrap_or_default();
        // Option A: force hv1 inline (dev) — bypass parser entirely
        if force_hv1_flag {
            let rc = nyash_rust::runner::hv1_inline::run_json_v1_inline(&json);
            println!("{}", rc);
            std::process::exit(rc);
        }
        // Minimal runner (no plugin init here); config parse is cheap and has no side effects.
        let cfg = CliConfig::parse();
        let runner = NyashRunner::new(cfg);
        if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
            eprintln!("[hv1-direct] early-exit (main)");
        }
        let rc = nyash_rust::runner::core_executor::run_json_v0(&runner, &json);
        println!("{}", rc);
        std::process::exit(rc);
    }

    // Bootstrap env overrides from nyash.toml [env] early (管理棟)
    env_config::bootstrap_from_toml_env();
    // Parse command-line arguments
    let config = CliConfig::parse();
    // Deprecation notice when invoked via legacy binary name
    if let Ok(exe) = std::env::current_exe() {
        if let Some(name) = exe.file_name().and_then(|s| s.to_str()) {
            if name.eq_ignore_ascii_case("nyash") {
                eprintln!("[deprecate] 'nyash' binary is deprecated. Please use 'hakorune'.");
            }
        }
    }
    // Legacy binary deprecation: prefer 'hakorune'
    if let Ok(exe) = std::env::current_exe() {
        if let Some(name) = exe.file_name().and_then(|s| s.to_str()) {
            let allow_legacy = std::env::var("HAKO_ALLOW_NYASH").ok().as_deref() == Some("1")
                || std::env::var("NYASH_ALLOW_NYASH").ok().as_deref() == Some("1");
            if name.eq_ignore_ascii_case("nyash") && !allow_legacy {
                eprintln!("[deprecate] 'nyash' binary is deprecated. Please use 'hakorune'.");
                std::process::exit(2);
            }
        }
    }
    // Create and run the execution coordinator
    let runner = NyashRunner::new(config);
    runner.run();
}

#[cfg(test)]
mod tests {

    use nyash_rust::box_trait::{NyashBox, StringBox};

    #[test]
    fn test_main_functionality() {
        // Smoke: library module path wiring works
        let string_box = StringBox::new("test".to_string());
        assert_eq!(string_box.to_string_box().value, "test");
        let _ = (); // CLI wiring exists via nyash_rust::cli
    }
}
