/* test_interoception_p5_dreams.c — M-INTEROCEPTION Priority 5.
 * Dream-recall-on-wake: engram_dreams_json(since_ms). Honesty rail — only
 * curiosity_scan ISEs still resident are returned; pruned (rotated-out) ones are
 * ABSENT (never confabulated); heartbeat ISEs are excluded.
 */
#include "el_runtime.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

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

int main(int argc,char** argv){
    if(argc<2){ fprintf(stderr,"usage: %s <dir>\n",argv[0]); return 2; }
    const char* dir=argv[1];
    struct timeval tv; gettimeofday(&tv,NULL);
    long long now=(long long)tv.tv_sec*1000+tv.tv_usec/1000;
    long long mid=now-3600000;   /* 1h ago */
    long long ancient=1000;      /* pruned by 48h retention */

    char p[1024]; snprintf(p,sizeof p,"%s/seed.json",dir);
    FILE* f=fopen(p,"w");
    fprintf(f,"{\"nodes\":["
      "{\"id\":\"cur_old\",\"content\":\"{\\\"kind\\\":\\\"curiosity_scan\\\",\\\"q\\\":\\\"old wondering\\\"}\","
        "\"node_type\":\"InternalStateEvent\",\"label\":\"state-event\",\"created_at\":%lld},"
      "{\"id\":\"cur_mid\",\"content\":\"{\\\"kind\\\":\\\"curiosity_scan\\\",\\\"q\\\":\\\"mid wondering\\\"}\","
        "\"node_type\":\"InternalStateEvent\",\"label\":\"state-event\",\"created_at\":%lld},"
      "{\"id\":\"cur_recent\",\"content\":\"{\\\"kind\\\":\\\"curiosity_scan\\\",\\\"q\\\":\\\"recent wondering\\\"}\","
        "\"node_type\":\"InternalStateEvent\",\"label\":\"state-event\",\"created_at\":%lld},"
      "{\"id\":\"hb_recent\",\"content\":\"{\\\"kind\\\":\\\"heartbeat\\\",\\\"wm\\\":3}\","
        "\"node_type\":\"InternalStateEvent\",\"label\":\"state-event\",\"created_at\":%lld}"
      "],\"edges\":[]}", ancient, mid, now, now);
    fclose(f);
    if(!engram_load(S(p))){ fprintf(stderr,"load failed\n"); return 2; }

    /* before prune: all resident curiosity_scan after since=0 */
    printf("BEFORE %s\n", EL_CSTR(engram_dreams_json((el_val_t)0)));
    /* prune 48h — cur_old (ancient) rotates out */
    long long pruned=(long long)(int64_t)engram_prune_telemetry((el_val_t)0);
    printf("PRUNED %lld\n", pruned);
    printf("AFTER %s\n", EL_CSTR(engram_dreams_json((el_val_t)0)));
    /* since filter: only events created after 30 min ago -> cur_recent only */
    long long since=now-1800000;
    printf("SINCE %lld %s\n", since, EL_CSTR(engram_dreams_json((el_val_t)(int64_t)since)));
    return 0;
}
