
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include "lib.h"
#include "uni_json_parser.h"
#include "uni_json_types.h"
#include "uni_json_p_binding.h"

extern struct uni_json_p_binding uni_json_c_bindings; // Esto usa la de la librería


/* Definición de las estructuras internas para los bindings de C */
struct uni_json_value {
    unsigned type;
    union {
        struct {
            struct uni_json_object_entry *first;
        } obj;
        struct {
            struct uni_json_array_entry *first;
        } ary;
        struct {
            uint8_t *s;
            size_t len;
        } s;
        double num;
        int b;
    } u;
};

struct uni_json_object_entry {
    struct uni_json_object_entry *next;
    uint8_t *key;
    struct uni_json_value *value;
};

struct uni_json_array_entry {
    struct uni_json_array_entry *next;
    struct uni_json_value *value;
};

int main() {
    char *input = "{\"ACTION\":\"add\", \"SUBSYSTEM\":\"input\"}";
    
    // El parser devuelve void*, lo casteamos a nuestra struct definida arriba
    struct uni_json_value *ev = uni_json_parse(
        (uint8_t *)input, 
        strlen(input), 
        10, 
        &uni_json_c_bindings, 
        NULL
    );

    if (!ev) return 1;

    if (ev->type == UJ_T_OBJ) {
        struct uni_json_object_entry *entry;
        
        // Ahora ev->u.obj.first ya es visible para el compilador
        for (entry = ev->u.obj.first; entry; entry = entry->next) {
            printf("Clave: %s -> ", (char *)entry->key);
            
            if (entry->value->type == UJ_T_STR) {
                // Acceso al string según la estructura definida
                printf("%s\n", (char *)entry->value->u.s.s);
            } else {
                printf("(otro tipo)\n");
            }
        }
    }

    // Liberar usando los bindings
    uni_json_c_bindings.free_object(ev);
    return 0;
}
