//! Pattern 2: Load a single Lua file as a plugin
//!
//! The .lua file path is passed as the first argument.
//! Useful for tools that load user-provided Lua scripts.
//!
//! Usage:
//!   cargo run --example plugin_file -- examples/hello.lua greet World
//!   cargo run --example plugin_file -- examples/hello.lua version

use senl::SenlApp;
use std::{env, process};

fn main() {
    let args: Vec<String> = env::args().collect();
    let lua_file = match args.get(1) {
        Some(f) if !f.starts_with('-') => f,
        _ => {
            eprintln!("usage: plugin_file <app.lua> [command] [args...]");
            process::exit(1);
        }
    };

    let remaining: Vec<&str> = args.iter().skip(2).map(|s| s.as_str()).collect();

    let code = SenlApp::from_file(lua_file)
        .unwrap_or_else(|e| {
            eprintln!("{e}");
            process::exit(1);
        })
        .run_with_args(remaining)
        .unwrap_or_else(|e| {
            eprintln!("{e}");
            process::exit(1);
        });
    process::exit(code);
}
