/* test_interoception_p0_emb.c — M-INTEROCEPTION Priority 0.
 *
 * Verifies the new READ-ONLY builtin engram_scan_nodes_emb_json(limit,offset):
 *   - every emitted node carries emb_dim and an emb JSON array of that length,
 *   - nodes without an embedding emit emb_dim:0 / emb:[],
 *   - pagination (limit/offset) is honoured,
 *   - the count matches engram_node_count,
 *   - the EXISTING engram_scan_nodes_json path is byte-unchanged (no emb field),
 *     i.e. the addition is purely additive / behavior-neutral.
 *
 * Pure-C harness (no elc). We craft a snapshot with real emb vectors, load it
 * (engram_load parses "emb" comma-lists into node->emb via eg_parse_emb), then
 * dump via both scan paths. Assertions live in run_interoception_p0.sh.
 */
#include "el_runtime.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static el_val_t S(const char* s){ return EL_STR(s); }

/* 16-d embedding as a comma list (>=8 required by eg_parse_emb). */
static void emb_list(char* out, size_t cap, int dim, double base){
    size_t o = 0;
    for (int i = 0; i < dim; i++){
        o += snprintf(out+o, cap-o, "%s%.4f", i?",":"", base + 0.01*i);
    }
}

int main(int argc, char** argv){
    if (argc < 2){ fprintf(stderr, "usage: %s <dir>\n", argv[0]); return 2; }
    const char* dir = argv[1];
    char snap[1024]; snprintf(snap, sizeof snap, "%s/seed.json", dir);

    char e1[512], e2[512];
    emb_list(e1, sizeof e1, 16, 0.10);
    emb_list(e2, sizeof e2, 16, 0.50);

    /* Two embedded nodes (distinct salience → deterministic sort order) and one
     * un-embedded node. */
    FILE* f = fopen(snap, "w");
    if (!f){ perror("fopen"); return 2; }
    fprintf(f,
      "{\"nodes\":["
        "{\"id\":\"n-high\",\"content\":\"high salience embedded\",\"node_type\":\"Concept\","
          "\"label\":\"emb-high\",\"tier\":\"Semantic\",\"salience\":0.9,\"importance\":0.8,"
          "\"confidence\":1.0,\"created_at\":1000,\"emb\":\"%s\"},"
        "{\"id\":\"n-mid\",\"content\":\"mid salience embedded\",\"node_type\":\"Concept\","
          "\"label\":\"emb-mid\",\"tier\":\"Semantic\",\"salience\":0.5,\"importance\":0.5,"
          "\"confidence\":1.0,\"created_at\":2000,\"emb\":\"%s\"},"
        "{\"id\":\"n-low\",\"content\":\"low salience no embedding\",\"node_type\":\"Fact\","
          "\"label\":\"noemb-low\",\"tier\":\"Semantic\",\"salience\":0.1,\"importance\":0.2,"
          "\"confidence\":1.0,\"created_at\":3000}"
      "],\"edges\":[]}", e1, e2);
    fclose(f);

    if (!engram_load(S(snap))){ fprintf(stderr, "load failed\n"); return 2; }
    long long nc = (long long)(int64_t)engram_node_count();
    printf("node_count=%lld\n", nc);

    /* full page */
    el_val_t all = engram_scan_nodes_emb_json((el_val_t)256, (el_val_t)0);
    char p[1024];
    snprintf(p, sizeof p, "%s/emb_all.json", dir);
    f = fopen(p, "w"); fputs(EL_CSTR(all), f); fclose(f);

    /* pagination: one node at offset 0 and one at offset 1 */
    el_val_t pg0 = engram_scan_nodes_emb_json((el_val_t)1, (el_val_t)0);
    el_val_t pg1 = engram_scan_nodes_emb_json((el_val_t)1, (el_val_t)1);
    snprintf(p, sizeof p, "%s/emb_pg0.json", dir); f = fopen(p, "w"); fputs(EL_CSTR(pg0), f); fclose(f);
    snprintf(p, sizeof p, "%s/emb_pg1.json", dir); f = fopen(p, "w"); fputs(EL_CSTR(pg1), f); fclose(f);

    /* existing path — must be unchanged / carry NO emb */
    el_val_t plain = engram_scan_nodes_json((el_val_t)256, (el_val_t)0);
    snprintf(p, sizeof p, "%s/plain.json", dir); f = fopen(p, "w"); fputs(EL_CSTR(plain), f); fclose(f);

    printf("wrote dumps to %s\n", dir);
    return 0;
}
