/* Thin JNIEXPORT shim over reading_json.c - keeps all real logic (and
 * every engine/interpreter header include) in plain, JNI-free C, same
 * separation watch/src/c/notify.c keeps from reading_state.c. */
#include <jni.h>
#include <stdlib.h>

#include "reading_json.h"

JNIEXPORT jstring JNICALL
Java_de_ladkau_deckinadash_nativebridge_DeckNative_generateReadingJson(
    JNIEnv *env, jobject thiz,
    jint birth_year, jint birth_month, jint birth_day,
    jint birth_hour, jint birth_minute, jdouble utc_offset_hours,
    jdouble latitude, jdouble longitude,
    jint gender, jlong utc_moment_epoch_seconds,
    jstring i18n_dir, jstring lang) {
  (void)thiz;

  BirthData birth = {
    .year = birth_year, .month = birth_month, .day = birth_day,
    .hour = birth_hour, .minute = birth_minute,
    .utc_offset_hours = utc_offset_hours,
    .latitude = latitude, .longitude = longitude,
  };

  const char *i18n_dir_chars = (*env)->GetStringUTFChars(env, i18n_dir, NULL);
  const char *lang_chars = (*env)->GetStringUTFChars(env, lang, NULL);

  char *json = NULL;
  int rc = reading_json_generate(&birth, (Gender)gender, (time_t)utc_moment_epoch_seconds,
                                  i18n_dir_chars, lang_chars, &json);

  (*env)->ReleaseStringUTFChars(env, i18n_dir, i18n_dir_chars);
  (*env)->ReleaseStringUTFChars(env, lang, lang_chars);

  if (rc != 0) {
    jclass exc = (*env)->FindClass(env, "java/lang/IllegalStateException");
    (*env)->ThrowNew(env, exc, "reading_json_generate failed");
    return NULL;
  }

  jstring result = (*env)->NewStringUTF(env, json);
  free(json);
  return result;
}
