mod register;
mod functions;

use std::ffi::c_void;
use mumu::parser::interpreter::Interpreter;
use register::hello::register_aws_hello;
use register::bye::register_aws_bye;
use register::credentials::register_aws_credentials;
use register::s3_list::register_s3_list;
use register::s3_upload::register_s3_upload;
use register::s3_fetch::register_s3_fetch;
use register::s3_create_bucket::register_s3_create_bucket;

#[no_mangle]
pub unsafe extern "C" fn Cargo_lock(
    interp_ptr: *mut c_void,
    _extra: *const c_void,
) -> i32 {
    let result = std::panic::catch_unwind(|| {
        if interp_ptr.is_null() {
            eprintln!("[aws-mumu] FATAL: NULL interpreter pointer");
            return -1;
        }
        let interp: &mut Interpreter = &mut *(interp_ptr as *mut Interpreter);

        // Register all necessary AWS functions with panic-protected bridges
        register_aws_hello(interp);
        register_aws_bye(interp);
        register_aws_credentials(interp);
        register_s3_list(interp);
        register_s3_upload(interp);
        register_s3_fetch(interp);
        register_s3_create_bucket(interp);

        0
    });
    match result {
        Ok(code) => code,
        Err(e) => {
            eprintln!("[aws-mumu] FATAL panic in Cargo_lock: {e:?}");
            if std::env::var("RUST_BACKTRACE").is_ok() {
                eprintln!("{:?}", std::backtrace::Backtrace::force_capture());
            }
            -101
        }
    }
}
