#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <node_api.h>

#include "fgmco.h"
#include "grp_2024_wrapper.h"


napi_value Grp2024(napi_env env, napi_callback_info info) {
  napi_status status;
  size_t argc = 4;
  napi_value args[4];
  
  // Get arguments from JavaScript
  status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
  assert(status == napi_ok);
  
  if (argc < 4) {
    napi_throw_error(env, NULL, "grp_2024 requires 4 arguments: rum_array, dirtab, ns, type_etab");
    return NULL;
  }
  
  // Extract and convert JavaScript arguments to C structures
  GRP_ENTREE_2024 grp_ent = {0};
  GRP_RESULTAT_2024 grp_res = {0};
  GHS_VALORISATION_2024 ghs_val = {0};
  RSS_INFO_2024 rss_inf = {0};
  
  // Extract RUM array (args[0])
  bool is_array;
  status = napi_is_array(env, args[0], &is_array);
  if (status != napi_ok || !is_array) {
    napi_throw_error(env, NULL, "First argument must be an array of RUM strings");
    return NULL;
  }
  
  uint32_t rum_array_length;
  status = napi_get_array_length(env, args[0], &rum_array_length);
  if (status != napi_ok) {
    napi_throw_error(env, NULL, "Failed to get RUM array length");
    return NULL;
  }
  
  if (rum_array_length > tab_max) {
    napi_throw_error(env, NULL, "Too many RUM entries in array");
    return NULL;
  }
  
  // Allocate and extract RUM strings from JavaScript array
  char* rum_strings[tab_max] = {0};
  for (uint32_t i = 0; i < rum_array_length; i++) {
    napi_value rum_element;
    status = napi_get_element(env, args[0], i, &rum_element);
    if (status != napi_ok) {
      // Clean up previously allocated strings
      for (uint32_t j = 0; j < i; j++) {
        if (rum_strings[j]) free(rum_strings[j]);
      }
      napi_throw_error(env, NULL, "Failed to get RUM array element");
      return NULL;
    }
    
    // Get string length
    size_t str_len;
    status = napi_get_value_string_utf8(env, rum_element, NULL, 0, &str_len);
    if (status != napi_ok) {
      // Clean up
      for (uint32_t j = 0; j < i; j++) {
        if (rum_strings[j]) free(rum_strings[j]);
      }
      napi_throw_error(env, NULL, "Failed to get RUM string length");
      return NULL;
    }
    
    // Allocate and copy string
    rum_strings[i] = (char*)malloc(str_len + 1);
    if (!rum_strings[i]) {
      // Clean up
      for (uint32_t j = 0; j < i; j++) {
        if (rum_strings[j]) free(rum_strings[j]);
      }
      napi_throw_error(env, NULL, "Memory allocation failed for RUM string");
      return NULL;
    }
    
    status = napi_get_value_string_utf8(env, rum_element, rum_strings[i], str_len + 1, &str_len);
    if (status != napi_ok) {
      // Clean up
      for (uint32_t j = 0; j <= i; j++) {
        if (rum_strings[j]) free(rum_strings[j]);
      }
      napi_throw_error(env, NULL, "Failed to extract RUM string");
      return NULL;
    }
    
    // Assign to grp_ent structure
    grp_ent.rum2[i] = rum_strings[i];
  }
  
  // Extract dirtab string (args[1])
  size_t str_length;
  status = napi_get_value_string_utf8(env, args[1], grp_ent.dirtab, sizeof(grp_ent.dirtab), &str_length);
  if (status != napi_ok) {
    // Clean up RUM strings
    for (uint32_t i = 0; i < rum_array_length; i++) {
      if (rum_strings[i]) free(rum_strings[i]);
    }
    napi_throw_error(env, NULL, "Failed to extract dirtab parameter");
    return NULL;
  }
  
  // Set ficum path based on dirtab
  sprintf(grp_ent.ficum, "%sficum.fg2024$.txt", grp_ent.dirtab);
  
  // Extract ns number (args[2])
  status = napi_get_value_int32(env, args[2], &grp_ent.ns);
  if (status != napi_ok) {
    // Clean up RUM strings
    for (uint32_t i = 0; i < rum_array_length; i++) {
      if (rum_strings[i]) free(rum_strings[i]);
    }
    napi_throw_error(env, NULL, "Failed to extract ns parameter");
    return NULL;
  }
  
  // Extract type_etab number (args[3])
  status = napi_get_value_int32(env, args[3], &grp_ent.type_etab);
  if (status != napi_ok) {
    // Clean up RUM strings
    for (uint32_t i = 0; i < rum_array_length; i++) {
      if (rum_strings[i]) free(rum_strings[i]);
    }
    napi_throw_error(env, NULL, "Failed to extract type_etab parameter");
    return NULL;
  }
  
  // Validate ns matches RUM array length
  if (grp_ent.ns != (int)rum_array_length) {
    // Clean up RUM strings
    for (uint32_t i = 0; i < rum_array_length; i++) {
      if (rum_strings[i]) free(rum_strings[i]);
    }
    napi_throw_error(env, NULL, "Number of stays (ns) must match RUM array length");
    return NULL;
  }
  
  
  // For real medical data, call the actual grouping function
  int ret = grp_2024(grp_ent, &grp_res, &ghs_val, &rss_inf);
  
  
  // Clean up RUM strings after processing
  for (uint32_t i = 0; i < rum_array_length; i++) {
    if (rum_strings[i]) free(rum_strings[i]);
  }
  
  // Create return object with real results
  napi_value return_obj;
  status = napi_create_object(env, &return_obj);
  assert(status == napi_ok);

  // Add return code
  napi_value ret_val;
  status = napi_create_int32(env, ret, &ret_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, return_obj, "returnCode", ret_val);
  assert(status == napi_ok);

  // grp_res
  napi_value grp_res_obj;
  status = napi_create_object(env, &grp_res_obj);
  assert(status == napi_ok);
  status = napi_set_named_property(env, return_obj, "grp_res", grp_res_obj);
  assert(status == napi_ok);

  // Add CMD to grp_res
  napi_value cmd_val;
  status = napi_create_string_utf8(env, grp_res.cmd, NAPI_AUTO_LENGTH, &cmd_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, grp_res_obj, "cmd", cmd_val);
  assert(status == napi_ok);
  
  // Add GHM to grp_res
  napi_value ghm_val;
  status = napi_create_string_utf8(env, grp_res.ghm, NAPI_AUTO_LENGTH, &ghm_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, grp_res_obj, "ghm", ghm_val);
  assert(status == napi_ok);
  
  // Add version to grp_res
  napi_value version_val;
  status = napi_create_string_utf8(env, grp_res.version, NAPI_AUTO_LENGTH, &version_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, grp_res_obj, "version", version_val);
  assert(status == napi_ok);
  
  // Add error code to grp_res
  napi_value cret_val;
  status = napi_create_string_utf8(env, grp_res.cret, NAPI_AUTO_LENGTH, &cret_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, grp_res_obj, "cret", cret_val);
  assert(status == napi_ok);

  // ghs_val
  napi_value ghs_val_obj;
  status = napi_create_object(env, &ghs_val_obj);
  assert(status == napi_ok);
  status = napi_set_named_property(env, return_obj, "ghs_val", ghs_val_obj);
  assert(status == napi_ok);

  // Add GHS to ghs_val
  napi_value ghs_val_ghs;
  status = napi_create_string_utf8(env, ghs_val.ghs, NAPI_AUTO_LENGTH, &ghs_val_ghs);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "ghs", ghs_val_ghs);
  assert(status == napi_ok);

  // Add nbseance
  napi_value nbseance_val;
  status = napi_create_int32(env, ghs_val.nbseance, &nbseance_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbseance", nbseance_val);
  assert(status == napi_ok);

  // Add nbjxsup
  napi_value nbjxsup_val;
  status = napi_create_int32(env, ghs_val.nbjxsup, &nbjxsup_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbjxsup", nbjxsup_val);
  assert(status == napi_ok);

  // Add sejxinf
  napi_value sejxinf_val;
  status = napi_create_int32(env, ghs_val.sejxinf, &sejxinf_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "sejxinf", sejxinf_val);
  assert(status == napi_ok);

  // Add nbjxinf
  napi_value nbjxinf_val;
  status = napi_create_int32(env, (int)ghs_val.nbjxinf, &nbjxinf_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbjxinf", nbjxinf_val);
  assert(status == napi_ok);

  // Add buhcd
  napi_value buhcd_val;
  status = napi_create_int32(env, ghs_val.buhcd, &buhcd_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "buhcd", buhcd_val);
  assert(status == napi_ok);

  // Add nbjrea
  napi_value nbjrea_val;
  status = napi_create_int32(env, ghs_val.nbjrea, &nbjrea_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbjrea", nbjrea_val);
  assert(status == napi_ok);

  // Add nbrep
  napi_value nbrep_val;
  status = napi_create_int32(env, ghs_val.nbrep, &nbrep_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbrep", nbrep_val);
  assert(status == napi_ok);

  // Add nbjsrc
  napi_value nbjsrc_val;
  status = napi_create_int32(env, ghs_val.nbjsrc, &nbjsrc_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbjsrc", nbjsrc_val);
  assert(status == napi_ok);

  // Add nbjstf_issuderea
  napi_value nbjstf_issuderea_val;
  status = napi_create_int32(env, ghs_val.nbjstf_issuderea, &nbjstf_issuderea_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbjstf_issuderea", nbjstf_issuderea_val);
  assert(status == napi_ok);

  // Add nbjstf_tot
  napi_value nbjstf_tot_val;
  status = napi_create_int32(env, ghs_val.nbjstf_tot, &nbjstf_tot_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbjstf_tot", nbjstf_tot_val);
  assert(status == napi_ok);

  // Add supplements counters
  napi_value nbac687_val, nbac688_val, nbac689_val;
  status = napi_create_int32(env, ghs_val.nbac687, &nbac687_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbac687", nbac687_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbac688, &nbac688_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbac688", nbac688_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbac689, &nbac689_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbac689", nbac689_val);
  assert(status == napi_ok);

  // Add nbprot and nbict
  napi_value nbprot_val, nbict_val;
  status = napi_create_int32(env, ghs_val.nbprot, &nbprot_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbprot", nbprot_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbict, &nbict_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbict", nbict_val);
  assert(status == napi_ok);

  // Add RX supplements
  napi_value nbacRX1_val, nbacRX2_val, nbacRX3_val, nbRCMI_val;
  status = napi_create_int32(env, ghs_val.nbacRX1, &nbacRX1_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbacRX1", nbacRX1_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbacRX2, &nbacRX2_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbacRX2", nbacRX2_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbacRX3, &nbacRX3_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbacRX3", nbacRX3_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbRCMI, &nbRCMI_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbRCMI", nbRCMI_val);
  assert(status == napi_ok);

  // Add additional supplements
  napi_value nbactsupp9623_val, nbactsupp9625_val, nbactsupp9631_val, nbactsupp9632_val, nbactsupp9633_val;
  status = napi_create_int32(env, ghs_val.nbactsupp9623, &nbactsupp9623_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbactsupp9623", nbactsupp9623_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbactsupp9625, &nbactsupp9625_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbactsupp9625", nbactsupp9625_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbactsupp9631, &nbactsupp9631_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbactsupp9631", nbactsupp9631_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbactsupp9632, &nbactsupp9632_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbactsupp9632", nbactsupp9632_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbactsupp9633, &nbactsupp9633_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbactsupp9633", nbactsupp9633_val);
  assert(status == napi_ok);

  // Add organ procurement indicator
  napi_value acprlvtorg_val;
  status = napi_create_int32(env, ghs_val.acprlvtorg, &acprlvtorg_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "acprlvtorg", acprlvtorg_val);
  assert(status == napi_ok);

  // Add neonatal days
  napi_value nbjnna_val, nbjnnb_val, nbjnnc_val;
  status = napi_create_int32(env, ghs_val.nbjnna, &nbjnna_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbjnna", nbjnna_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbjnnb, &nbjnnb_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbjnnb", nbjnnb_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbjnnc, &nbjnnc_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbjnnc", nbjnnc_val);
  assert(status == napi_ok);

  // Add GHS before innovation
  napi_value ghsavantInnov_val;
  status = napi_create_string_utf8(env, ghs_val.ghsavantInnov, NAPI_AUTO_LENGTH, &ghsavantInnov_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "ghsavantInnov", ghsavantInnov_val);
  assert(status == napi_ok);

  // Add dialysis supplements
  napi_value nbhd_val, nbent1_val, nbent2_val, nbent3_val;
  status = napi_create_int32(env, ghs_val.nbhd, &nbhd_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbhd", nbhd_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbent1, &nbent1_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbent1", nbent1_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbent2, &nbent2_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbent2", nbent2_val);
  assert(status == napi_ok);

  status = napi_create_int32(env, ghs_val.nbent3, &nbent3_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbent3", nbent3_val);
  assert(status == napi_ok);

  // Add hyperbaric chambers
  napi_value nbchypb_val;
  status = napi_create_int32(env, ghs_val.nbchypb, &nbchypb_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbchypb", nbchypb_val);
  assert(status == napi_ok);

  // Add antepartum supplements
  napi_value nbantepartum_val;
  status = napi_create_int32(env, ghs_val.nbantepartum, &nbantepartum_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbantepartum", nbantepartum_val);
  assert(status == napi_ok);

  // Add radiotherapy supplements
  napi_value nbradiopedia_val;
  status = napi_create_int32(env, ghs_val.nbradiopedia, &nbradiopedia_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbradiopedia", nbradiopedia_val);
  assert(status == napi_ok);

  // Add GHS minor indicator
  napi_value ghsminor_val;
  status = napi_create_int32(env, ghs_val.ghsminor, &ghsminor_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "ghsminor", ghsminor_val);
  assert(status == napi_ok);

  // Add SDC supplement
  napi_value supplSDC_val;
  status = napi_create_int32(env, ghs_val.supplSDC, &supplSDC_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "supplSDC", supplSDC_val);
  assert(status == napi_ok);

  // Add CarTCell supplement
  napi_value supplCarTCell_val;
  status = napi_create_int32(env, ghs_val.supplCarTCell, &supplCarTCell_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "supplCarTCell", supplCarTCell_val);
  assert(status == napi_ok);

  // Add Avastin flag
  napi_value flagAvastin_val;
  status = napi_create_int32(env, ghs_val.flagAvastin, &flagAvastin_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "flagAvastin", flagAvastin_val);
  assert(status == napi_ok);

  // Add pediatric protontherapy
  napi_value nbprotped_val;
  status = napi_create_int32(env, ghs_val.nbprotped, &nbprotped_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, ghs_val_obj, "nbprotped", nbprotped_val);
  assert(status == napi_ok);

   // rss_inf
  napi_value rss_inf_obj;
  status = napi_create_object(env, &rss_inf_obj);
  assert(status == napi_ok);
  status = napi_set_named_property(env, return_obj, "rss_inf", rss_inf_obj);
  assert(status == napi_ok);

  // Add agea (age in years)
  napi_value agea_val;
  status = napi_create_int32(env, rss_inf.agea, &agea_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, rss_inf_obj, "agea", agea_val);
  assert(status == napi_ok);

  // Add agej (age in days)
  napi_value agej_val;
  status = napi_create_int32(env, rss_inf.agej, &agej_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, rss_inf_obj, "agej", agej_val);
  assert(status == napi_ok);

  // Add dstot (total stay duration)
  napi_value dstot_val;
  status = napi_create_int32(env, rss_inf.dstot, &dstot_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, rss_inf_obj, "dstot", dstot_val);
  assert(status == napi_ok);

  // Add diaglist object
  napi_value diaglist_obj;
  status = napi_create_object(env, &diaglist_obj);
  assert(status == napi_ok);
  status = napi_set_named_property(env, rss_inf_obj, "diaglist", diaglist_obj);
  assert(status == napi_ok);

  // Add nbdiag to diaglist
  napi_value nbdiag_val;
  status = napi_create_int32(env, rss_inf.diaglist.nbdiag, &nbdiag_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, diaglist_obj, "nbdiag", nbdiag_val);
  assert(status == napi_ok);

  // Add listdiag to diaglist (if available)
  if (rss_inf.diaglist.listdiag != NULL) {
    napi_value listdiag_val;
    status = napi_create_string_utf8(env, rss_inf.diaglist.listdiag, NAPI_AUTO_LENGTH, &listdiag_val);
    assert(status == napi_ok);
    status = napi_set_named_property(env, diaglist_obj, "listdiag", listdiag_val);
    assert(status == napi_ok);
  }

  // Add actlist object
  napi_value actlist_obj;
  status = napi_create_object(env, &actlist_obj);
  assert(status == napi_ok);
  status = napi_set_named_property(env, rss_inf_obj, "actlist", actlist_obj);
  assert(status == napi_ok);

  // Add nbac to actlist
  napi_value nbac_val;
  status = napi_create_int32(env, rss_inf.actlist.nbac, &nbac_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, actlist_obj, "nbac", nbac_val);
  assert(status == napi_ok);

  // Add listzac to actlist (if available)
  if (rss_inf.actlist.listzac != NULL) {
    napi_value listzac_val;
    status = napi_create_string_utf8(env, rss_inf.actlist.listzac, NAPI_AUTO_LENGTH, &listzac_val);
    assert(status == napi_ok);
    status = napi_set_named_property(env, actlist_obj, "listzac", listzac_val);
    assert(status == napi_ok);
  }

  // Add tabval array (INFO_RUM_2024 for each RUM)
  napi_value tabval_array;
  status = napi_create_array_with_length(env, rum_array_length, &tabval_array);
  assert(status == napi_ok);
  status = napi_set_named_property(env, rss_inf_obj, "tabval", tabval_array);
  assert(status == napi_ok);

  for (uint32_t i = 0; i < rum_array_length; i++) {
    napi_value tabval_element;
    status = napi_create_object(env, &tabval_element);
    assert(status == napi_ok);

    // Add brea (boolean indicator for ICU marker acts)
    napi_value brea_val;
    status = napi_create_int32(env, rss_inf.tabval[i].brea, &brea_val);
    assert(status == napi_ok);
    status = napi_set_named_property(env, tabval_element, "brea", brea_val);
    assert(status == napi_ok);

    // Add bsc (boolean indicator for continuous monitoring)
    napi_value bsc_val;
    status = napi_create_int32(env, rss_inf.tabval[i].bsc, &bsc_val);
    assert(status == napi_ok);
    status = napi_set_named_property(env, tabval_element, "bsc", bsc_val);
    assert(status == napi_ok);

    // Add typum (medical unit authorization type)
    napi_value typum_val;
    status = napi_create_string_utf8(env, rss_inf.tabval[i].typum, NAPI_AUTO_LENGTH, &typum_val);
    assert(status == napi_ok);
    status = napi_set_named_property(env, tabval_element, "typum", typum_val);
    assert(status == napi_ok);

    // Add dsp (partial stay duration)
    napi_value dsp_val;
    status = napi_create_int32(env, rss_inf.tabval[i].dsp, &dsp_val);
    assert(status == napi_ok);
    status = napi_set_named_property(env, tabval_element, "dsp", dsp_val);
    assert(status == napi_ok);

    status = napi_set_element(env, tabval_array, i, tabval_element);
    assert(status == napi_ok);
  }

  // Add signature fields
  napi_value sigfg_val;
  status = napi_create_string_utf8(env, rss_inf.sigfg, NAPI_AUTO_LENGTH, &sigfg_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, rss_inf_obj, "sigfg", sigfg_val);
  assert(status == napi_ok);

  napi_value sigrss_val;
  status = napi_create_string_utf8(env, rss_inf.sigrss, NAPI_AUTO_LENGTH, &sigrss_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, rss_inf_obj, "sigrss", sigrss_val);
  assert(status == napi_ok);

  napi_value sigrssghsfg_val;
  status = napi_create_string_utf8(env, rss_inf.sigrssghsfg, NAPI_AUTO_LENGTH, &sigrssghsfg_val);
  assert(status == napi_ok);
  status = napi_set_named_property(env, rss_inf_obj, "sigrssghsfg", sigrssghsfg_val);
  assert(status == napi_ok);

  return return_obj;
}

#define DECLARE_NAPI_METHOD(name, func)                                        \
  { name, 0, func, 0, 0, 0, napi_default, 0 }

napi_value Init(napi_env env, napi_value exports) {
  napi_status status;
  napi_property_descriptor desc[] = {
    DECLARE_NAPI_METHOD("grp_2024_wrapper", Grp2024)
  };
  status = napi_define_properties(env, exports, 1, desc);
  assert(status == napi_ok);
  return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) 