// test_jni_native.cpp — teste libkazeia_engine.so bout-en-bout SANS JVM:
// dlopen + JNIEnv mock (GetStringUTFChars/Release/NewStringUTF) -> load / generateRaw multi-tour / free.
// Valide le marshalling JNI réel + le flux option C sur device.
// Build: NDK clang++ -Iinclude jni/test_jni_native.cpp -ldl -o test_jni_native
// Run: GGML_HEXAGON_GDN_PREFILL=1 LD_LIBRARY_PATH=lib ADSP_LIBRARY_PATH=lib ./test_jni_native lib/libkazeia_engine.so model.gguf
#include <jni.h>
#include <dlfcn.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>

static const char* GetStringUTFChars_impl(JNIEnv*, jstring s, jboolean* c){ if(c)*c=0; return (const char*)s; }
static void        ReleaseStringUTFChars_impl(JNIEnv*, jstring, const char*){}
static jstring     NewStringUTF_impl(JNIEnv*, const char* u){ return (jstring) strdup(u?u:""); }

int main(int argc, char** argv){
    if(argc<3){ printf("usage: test_jni_native lib.so model.gguf\n"); return 1; }
    void* lib = dlopen(argv[1], RTLD_NOW|RTLD_GLOBAL);
    if(!lib){ printf("dlopen fail: %s\n", dlerror()); return 1; }

    auto fload = (jlong(*)(JNIEnv*,jobject,jstring,jint)) dlsym(lib, "Java_com_kazeia_llm_EngineJni_load");
    auto fraw  = (jstring(*)(JNIEnv*,jobject,jlong,jstring,jint)) dlsym(lib, "Java_com_kazeia_llm_EngineJni_generateRaw");
    auto fgen  = (jstring(*)(JNIEnv*,jobject,jlong,jstring,jstring,jint)) dlsym(lib, "Java_com_kazeia_llm_EngineJni_generate");
    auto ffree = (void(*)(JNIEnv*,jobject,jlong)) dlsym(lib, "Java_com_kazeia_llm_EngineJni_free");
    if(!fload||!fraw||!fgen||!ffree){ printf("dlsym fail (%p %p %p %p)\n",(void*)fload,(void*)fraw,(void*)fgen,(void*)ffree); return 1; }

    JNINativeInterface iface; memset(&iface,0,sizeof iface);
    iface.GetStringUTFChars     = GetStringUTFChars_impl;
    iface.ReleaseStringUTFChars = ReleaseStringUTFChars_impl;
    iface.NewStringUTF          = NewStringUTF_impl;
    const JNINativeInterface* pf = &iface;
    JNIEnv* env = (JNIEnv*) &pf;

    jlong h = fload(env, nullptr, (jstring) argv[2], 4096);
    printf("load -> h=%p\n", (void*)h);
    if(!h){ printf("load fail\n"); return 1; }

    // mono-tour
    const char* sys = "Tu es Kazeia, soutien psy bref en francais, tutoiement.";
    const char* usr = "Je n'arrive plus a dormir, je rumine la nuit.";
    jstring r1 = fgen(env, nullptr, h, (jstring) sys, (jstring) usr, 60);
    printf("\n[generate mono-tour]\n%s\n", (const char*) r1);

    // multi-tour: prompt ChatML complet (system + 1 echange + nouvelle question memoire)
    std::string p =
      "<|im_start|>system\nTu es Kazeia, soutien psy, francais, tutoiement.<|im_end|>\n"
      "<|im_start|>user\nJe m'appelle Marc et je dors mal.<|im_end|>\n"
      "<|im_start|>assistant\nMarc, je comprends, le manque de sommeil est epuisant.<|im_end|>\n"
      "<|im_start|>user\nRappelle-moi mon prenom ?<|im_end|>\n"
      "<|im_start|>assistant\n<think>\n\n</think>\n\n";
    jstring r2 = fraw(env, nullptr, h, (jstring) p.c_str(), 40);
    printf("\n[generateRaw multi-tour, test memoire]\n%s\n", (const char*) r2);

    ffree(env, nullptr, h);
    printf("\nfree OK\n");
    return 0;
}
