/*!
 * Runner plugin initialization (extracted from runner.rs)
 *
 * Purpose: Initialize v2 plugin system from nyash.toml and apply config
 * Behavior: Quiet by default; use NYASH_CLI_VERBOSE=1 or NYASH_DEBUG_PLUGIN=1 for logs
 */

use crate::runtime::{
    get_global_plugin_host, get_global_registry, init_global_plugin_host, PluginConfig,
};

fn resolve_plugin_toml() -> String {
    // Prefer hakorune.toml, fallback to nyash.toml (check CWD, then NYASH_ROOT)
    let cwd_hako = std::path::Path::new("hakorune.toml");
    if cwd_hako.exists() {
        return "hakorune.toml".to_string();
    }
    let cwd_ny = std::path::Path::new("nyash.toml");
    if cwd_ny.exists() {
        return "nyash.toml".to_string();
    }
    if let Ok(root) = std::env::var("NYASH_ROOT") {
        let p = std::path::Path::new(&root).join("hakorune.toml");
        if p.exists() {
            return p.to_string_lossy().to_string();
        }
        let p2 = std::path::Path::new(&root).join("nyash.toml");
        if p2.exists() {
            return p2.to_string_lossy().to_string();
        }
    }
    "nyash.toml".to_string()
}

pub fn init_bid_plugins() {
    let cli_verbose = std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1");
    let plugin_debug = std::env::var("NYASH_DEBUG_PLUGIN").ok().as_deref() == Some("1");
    if plugin_debug {
        eprintln!("🔍 DEBUG: Initializing v2 plugin system");
    }

    let cfg_path = resolve_plugin_toml();
    match init_global_plugin_host(&cfg_path) {
        Ok(()) => {
            if plugin_debug || cli_verbose {
                eprintln!("[plugin/init] plugin host initialized from {}", cfg_path);
                // Show which plugin loader backend compiled in (enabled/stub)
                println!(
                    "[plugin-loader] backend={}",
                    crate::runtime::plugin_loader_v2::backend_kind()
                );
            }
            let host = get_global_plugin_host();
            let host = host.read().unwrap();
            if let Some(config) = host.config_ref() {
                let registry = get_global_registry();
                for (lib_name, lib_def) in &config.libraries {
                    for box_name in &lib_def.boxes {
                        if plugin_debug {
                            eprintln!("  📦 Registering plugin provider for {}", box_name);
                        }
                        registry.apply_plugin_config(&PluginConfig {
                            plugins: [(box_name.clone(), lib_name.clone())].into(),
                        });
                    }
                }
                if plugin_debug || cli_verbose {
                    eprintln!("[plugin/init] ✅ plugin host fully configured");
                }
            }

            // Optional autoload for [using.*] kind="dylib" packages
            if std::env::var("NYASH_USING_DYLIB_AUTOLOAD").ok().as_deref() == Some("1")
                && std::env::var("NYASH_DISABLE_PLUGINS").ok().as_deref() != Some("1")
            {
                if plugin_debug || cli_verbose {
                    eprintln!("[using.dylib/autoload] scanning nyash.toml packages …");
                }
                let mut using_paths: Vec<String> = Vec::new();
                let mut pending_modules: std::vec::Vec<(String, String)> = Vec::new();
                let mut aliases: std::collections::HashMap<String, String> =
                    std::collections::HashMap::new();
                let mut packages: std::collections::HashMap<
                    String,
                    crate::using::spec::UsingPackage,
                > = std::collections::HashMap::new();
                let _ = crate::using::resolver::populate_from_toml(
                    &mut using_paths,
                    &mut pending_modules,
                    &mut aliases,
                    &mut packages,
                );
                for (name, pkg) in packages.iter() {
                    if let crate::using::spec::PackageKind::Dylib = pkg.kind {
                        // Build library name from file stem (best-effort)
                        let lib_name = std::path::Path::new(&pkg.path)
                            .file_name()
                            .and_then(|s| s.to_str())
                            .unwrap_or(name)
                            .to_string();
                        let host = get_global_plugin_host();
                        let res =
                            host.read()
                                .unwrap()
                                .load_library_direct(&lib_name, &pkg.path, &[]);
                        if let Err(e) = res {
                            if plugin_debug || cli_verbose {
                                eprintln!("[using.dylib/autoload] failed '{}': {}", lib_name, e);
                            }
                        } else if plugin_debug || cli_verbose {
                            eprintln!(
                                "[using.dylib/autoload] loaded '{}' from {}",
                                lib_name, pkg.path
                            );
                        }
                    }
                }
            }
        }
        Err(e) => {
            if plugin_debug || cli_verbose {
                eprintln!(
                    "[plugin/init] failed to initialize from {}: {}",
                    cfg_path, e
                );
            } else {
                eprintln!(
                    "[plugin/init] plugins disabled (config={}): {}",
                    cfg_path, e
                );
            }
            return;
        }
    }
}
