// Copyright 2026 Jordan Castro <jordan@grupojvm.com>
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//
// cargo test --test test_call --no-default-features --features "lua,python,js,include-core" -- --nocapture --test-threads=1

#[allow(unused)]
#[cfg(test)]
mod tests {
    use pixelscript::{
        own_var, pxs_addfunc, pxs_addmod, pxs_call,
        pxs_debugvar, pxs_exec, pxs_finalize, pxs_freearena, pxs_freevar, pxs_getint,
        pxs_initialize, pxs_listadd, pxs_listget, pxs_newarena, pxs_newexception, pxs_newint,
        pxs_newlist, pxs_newmod, pxs_newnull,
        shared::{
            pxs_Runtime,
            var::{pxs_Var, pxs_VarT},
        },
    };
    use etffi::{cstring::CStringSafe, borrow_string, create_raw_string, free_raw_string, own_string, ptr_magic::PtrMagic};

    extern "C" fn anything(args: pxs_VarT) -> pxs_VarT {
        let mn = create_raw_string!("add");
        let iargs = pxs_newlist();
        pxs_listadd(iargs, pxs_newint(1));
        pxs_listadd(iargs, pxs_newint(2));
        let res = pxs_call(pxs_listget(args, 0), mn, iargs);
        println!("{}", own_string!(pxs_debugvar(res)));
        if pxs_getint(res) != 3 {
            pxs_freevar(res);
            unsafe {
                free_raw_string!(mn);
            }
            let mut cstrgen = CStringSafe::new();
            return pxs_newexception(cstrgen.new_string("ANSWER IS NOT 3!"));
        }
        // assert!(pxs_getint(res) == 3, "We could not run the add function");
        pxs_freevar(res);
        unsafe {
            free_raw_string!(mn);
        }
        return pxs_newnull();
    }

    fn print_helper(lang: &str) {
        println!("====================== {lang} ===================");
    }

    fn test_python() {
        let script = create_raw_string!(
            r#"
from pxs import *
def add(n1, n2):
    return n1 + n2

anything(1,2)
"#
        );
        let file_name = create_raw_string!("<test>");

        let err = own_var!(pxs_exec(pxs_Runtime::pxs_Python, script, file_name));

        unsafe {
            free_raw_string!(script);
            free_raw_string!(file_name);
        };

        assert!(
            err.is_null(),
            "Error is not empty: {}",
            err.get_string().unwrap()
        );
    }

    fn test_lua() {
        let script = create_raw_string!(
            r#"
local pxs = require('pxs')
function add(n1, n2)
    return n1 + n2
end
pxs.anything(1,2)
"#
        );
        let file_name = create_raw_string!("<test>");

        let err = own_var!(pxs_exec(pxs_Runtime::pxs_Lua, script, file_name));

        unsafe {
            free_raw_string!(script);
            free_raw_string!(file_name);
        };

        assert!(
            err.is_null(),
            "Error is not empty: {}",
            err.get_string().unwrap()
        );
    }

    fn test_js() {
        let script = create_raw_string!(
            r#"
import * as pxs from 'pxs';
globalThis.add = (n1, n2) => n1 + n2;

pxs.anything(1,2);
"#
        );
        let file_name = create_raw_string!("<test>");

        let err = own_var!(pxs_exec(pxs_Runtime::pxs_JavaScript, script, file_name));

        unsafe {
            free_raw_string!(script);
            free_raw_string!(file_name);
        };

        assert!(
            err.is_null(),
            "Error is not empty: {}",
            err.get_string().unwrap()
        );
    }

    #[test]
    fn test_call() {
        pxs_initialize();

        let mod_name = create_raw_string!("pxs");
        let module = pxs_newmod(mod_name);
        let anything_name = create_raw_string!("anything");
        pxs_addfunc(module, anything_name, anything);
        pxs_addmod(module);

        unsafe {
            free_raw_string!(mod_name);
            free_raw_string!(anything_name);
        }

        print_helper("Python");
        test_python();
        print_helper("Lua");
        test_lua();
        print_helper("JS");
        test_js();

        pxs_finalize();
    }
}
