/* Closed-form unit tests for the VERIFIER layer (engram_verify.c). Every case is a
 * hand-built synthetic descriptor / claim point whose verdict is known in closed
 * form — the checks are PROVEN, not declared. ASan/UBSan target.
 *
 * The headline case is CONSISTENCY's polarity check: the reassurance→accusation
 * inversion ("you never fought" → "you argued") that no grammar check catches. */
#include "engram_verify.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

static int failures = 0, checks = 0;
static void ok(const char* what, int cond) {
    checks++;
    if (!cond) { failures++; printf("  FAIL: %s\n", what); }
    else        printf("  ok:   %s\n", what);
}
static void approx(const char* what, double got, double exp, double tol) {
    ok(what, fabs(got - exp) <= tol);
    if (fabs(got - exp) > tol) printf("        got=%.9g exp=%.9g\n", got, exp);
}

/* ── descriptor builder (mirrors test_reason.c) ─────────────────────────────── */
static float* vec(const double* v, int dim) {
    float* f = malloc((size_t)dim * sizeof(float));
    for (int i = 0; i < dim; i++) f[i] = (float)v[i];
    return f;
}
static GeoDescriptor* mk(int dim, const double* centroid,
                         int n_axes, const double* axis_flat, const double* extents,
                         int n_members, const char** ids, double total_var) {
    GeoDescriptor* g = calloc(1, sizeof(GeoDescriptor));
    g->dim = dim;
    g->centroid = centroid ? vec(centroid, dim) : NULL;
    g->global_mean = NULL;
    g->n_axes = n_axes;
    g->axes = n_axes ? calloc((size_t)n_axes, sizeof(GeoAxis)) : NULL;
    double tr = 0;
    for (int k = 0; k < n_axes; k++) {
        g->axes[k].axis = vec(&axis_flat[(size_t)k * dim], dim);
        g->axes[k].extent = extents[k];
        tr += extents[k] * extents[k];
    }
    g->total_variance = (total_var >= 0) ? total_var : tr;
    g->radius = sqrt(g->total_variance > 0 ? g->total_variance : 0);
    g->n_members = n_members; g->n_embedded = n_members;
    g->members = n_members ? calloc((size_t)n_members, sizeof(GeoMember)) : NULL;
    for (int i = 0; i < n_members; i++) {
        g->members[i].id = strdup(ids[i]);
        g->members[i].membership = 1.0;
        g->members[i].centrality = (double)(n_members - i);
        g->members[i].embedded = 1;
    }
    g->hub_id = n_members ? strdup(ids[0]) : strdup("");
    g->k_core = 1; g->co_registration = 0.0; g->n_edges = 0; g->edges = NULL;
    return g;
}

int main(void) {
    printf("== VERIFIER layer unit tests ==\n");

    /* ══════════════════ GROUNDING — supported vs floating (hallucination) ════ */
    /* Two real neighborhoods: E0 at origin, E1 far along e0. A claim planted inside
     * E0 is grounded; a claim floating far off-manifold (along an unmodeled axis) is
     * flagged UNGROUNDED; a claim near E1 grounds to E1, not E0. */
    {
        int dim = 4;
        double c0[4] = {0,0,0,0}, c1[4] = {10,0,0,0};
        double ax[8] = {1,0,0,0, 0,1,0,0}; double ex[2] = {1,1};
        const char* i0[1] = {"E0"}, *i1[1] = {"E1"};
        GeoDescriptor* E0 = mk(dim, c0, 2, ax, ex, 1, i0, -1);
        GeoDescriptor* E1 = mk(dim, c1, 2, ax, ex, 1, i1, -1);
        const GeoDescriptor* ev[2] = {E0, E1};

        /* (1) grounded claim — sits inside E0. */
        float in[4] = {0.3f, -0.2f, 0, 0};
        GeoGrounding g1;
        int rc = engram_verify_grounding(in, dim, ev, 2, 1.0, 0.5, &g1);
        ok("grounding returns 0", rc == 0);
        printf("[grounding] IN   score=%.4f grounded=%d best=%d dist=%.3f ortho=%.3f nearL2=%.3f\n",
               g1.grounding, g1.grounded, g1.best, g1.best_distance, g1.best_ortho, g1.nearest_centroid_l2);
        ok("planted-inside claim is GROUNDED", g1.grounded == 1);
        ok("grounds to the nearest structure E0", g1.best == 0);
        ok("grounded score high (>0.7)", g1.grounding > 0.7);
        approx("off-model residual ~0 for in-distribution claim", g1.best_ortho, 0.0, 1e-4);
        engram_verify_grounding_free(&g1);

        /* (2) hallucinated claim — floats far along the unmodeled e2 axis. */
        float out[4] = {0, 0, 50.0f, 0};
        GeoGrounding g2;
        engram_verify_grounding(out, dim, ev, 2, 1.0, 0.5, &g2);
        printf("[grounding] OUT  score=%.6f grounded=%d best=%d dist=%.3f ortho=%.3f nearL2=%.3f\n",
               g2.grounding, g2.grounded, g2.best, g2.best_distance, g2.best_ortho, g2.nearest_centroid_l2);
        ok("floating claim is FLAGGED (ungrounded)", g2.grounded == 0);
        ok("floating claim scores near zero (<0.01)", g2.grounding < 0.01);
        ok("off-model residual is large (the hallucination signal)", g2.best_ortho > 40.0);
        ok("nearest real structure is far (L2>40)", g2.nearest_centroid_l2 > 40.0);
        engram_verify_grounding_free(&g2);

        /* (3) selection — a claim near E1 grounds to E1. */
        float nearE1[4] = {9.8f, 0.1f, 0, 0};
        GeoGrounding g3;
        engram_verify_grounding(nearE1, dim, ev, 2, 1.0, 0.5, &g3);
        printf("[grounding] E1   score=%.4f grounded=%d best=%d\n", g3.grounding, g3.grounded, g3.best);
        ok("claim near E1 grounds to E1 (best=1)", g3.best == 1 && g3.grounded == 1);
        engram_verify_grounding_free(&g3);

        engram_geo_free(E0); engram_geo_free(E1);
    }

    /* ══════════════════ CONSISTENCY (a) — THE NEGATION-INVERSION CATCH ═══════ */
    /* The motivating failure, geometrically. Polarity axis along e0:
     *   pole_pos = the AFFIRM region ("argued / fought")     centroid (+5, …)
     *   pole_neg = the NEGATE region ("never fought / at peace") centroid (−5, …)
     * The grounded TRUTH (context) is the reassurance "you never fought" → sits on
     * the NEGATE side (−5). The bad translation CLAIM "you argued" lands on the
     * AFFIRM side (+4). Opposite sides of the negation axis ⇒ INVERSION flagged —
     * even though "you argued" is perfectly grammatical. This is the catch. */
    {
        int dim = 4;
        double c_pos[4] = { 5, 0, 0, 0};      /* "argued / fought"        */
        double c_neg[4] = {-5, 0, 0, 0};      /* "never fought / at peace"*/
        double c_truth[4] = {-5, 0, 0, 0};    /* context: the reassurance */
        double ax[4] = {1,0,0,0}; double ex[1] = {1};
        const char* ip[1]={"pos"},*in[1]={"neg"},*it[1]={"truth"};
        GeoDescriptor* POS = mk(dim, c_pos, 1, ax, ex, 1, ip, -1);
        GeoDescriptor* NEG = mk(dim, c_neg, 1, ax, ex, 1, in, -1);
        GeoDescriptor* CTX = mk(dim, c_truth, 1, ax, ex, 1, it, -1);

        /* the plausible LIE: "you argued" — grammatical, fluent, and INVERTED. */
        float lie[4] = { 4, 0, 0, 0};
        GeoConsistency cl;
        int rc = engram_verify_consistency(lie, dim, CTX, POS, NEG, NULL,
                                           1.0, 0.10, 0.5, 0.0, &cl);
        ok("consistency returns 0", rc == 0);
        printf("[consistency] LIE  verdict=%d inverted=%d claim_side=%.3f ref_side=%.3f sep=%.3f consist=%.3f\n",
               cl.verdict, cl.inverted, cl.polarity_claim, cl.polarity_reference, cl.polarity_separation, cl.consistency);
        ok("NEGATION INVERSION caught (inverted=1)", cl.inverted == 1);
        ok("verdict = POLARITY", cl.verdict == GEO_CONSIST_POLARITY);
        ok("claim sits on the AFFIRM pole (+)", cl.polarity_claim > 0);
        ok("truth sits on the NEGATE pole (−)", cl.polarity_reference < 0);
        ok("consistency collapses to 0 on inversion", cl.consistency < 1e-9);

        /* the FAITHFUL translation: "you were at peace" — same pole as the truth. */
        float ok_claim[4] = {-4, 0, 0, 0};
        GeoConsistency cok;
        engram_verify_consistency(ok_claim, dim, CTX, POS, NEG, NULL,
                                  1.0, 0.10, 0.5, 0.0, &cok);
        printf("[consistency] TRUE verdict=%d inverted=%d claim_side=%.3f consist=%.3f\n",
               cok.verdict, cok.inverted, cok.polarity_claim, cok.consistency);
        ok("faithful claim NOT flagged (inverted=0)", cok.inverted == 0);
        ok("faithful claim verdict OK", cok.verdict == GEO_CONSIST_OK);
        ok("faithful claim consistency = 1", cok.consistency > 0.999);

        /* a NEUTRAL claim near the midpoint must NOT false-trigger. */
        float neutral[4] = {0.1f, 0, 0, 0};   /* |side|=0.1 < deadzone 0.5 */
        GeoConsistency cn;
        engram_verify_consistency(neutral, dim, CTX, POS, NEG, NULL,
                                  1.0, 0.10, 0.5, 0.0, &cn);
        printf("[consistency] NEUT verdict=%d inverted=%d claim_side=%.3f consist=%.3f\n",
               cn.verdict, cn.inverted, cn.polarity_claim, cn.consistency);
        ok("neutral claim inside deadzone does NOT trigger inversion", cn.inverted == 0);

        engram_geo_free(POS); engram_geo_free(NEG); engram_geo_free(CTX);
    }

    /* ══════════════════ CONSISTENCY (b) — GEOMETRIC contradiction ════════════ */
    /* A claim that sits INSIDE a forbidden region it should be far from, and a claim
     * that violates a max-distance constraint to its context, are both flagged. */
    {
        int dim = 4;
        double c_ctx[4] = {0,0,0,0};
        double c_forb[4] = {0,10,0,0};        /* forbidden region, offset along e1 */
        double ax[8] = {0,1,0,0, 1,0,0,0}; double ex[2] = {1,1};
        const char* ic[1]={"ctx"},*ifb[1]={"forb"};
        GeoDescriptor* CTX  = mk(dim, c_ctx, 2, ax, ex, 1, ic, -1);
        GeoDescriptor* FORB = mk(dim, c_forb, 2, ax, ex, 1, ifb, -1);

        /* claim sitting inside the forbidden region → geometric contradiction. */
        float inside[4] = {0, 10.1f, 0, 0};
        GeoConsistency cf;
        engram_verify_consistency(inside, dim, CTX, NULL, NULL, FORB,
                                  1.0, 0.10, 0.5, 0.0, &cf);
        printf("[consistency] FORB verdict=%d geo_viol=%d forb_fit=%.4f consist=%.3f\n",
               cf.verdict, cf.geo_violation, cf.forbidden_fit, cf.consistency);
        ok("claim inside forbidden region FLAGGED", cf.geo_violation == 1);
        ok("verdict = GEOMETRIC", cf.verdict == GEO_CONSIST_GEOMETRIC);
        ok("forbidden fit is high (claim really is inside)", cf.forbidden_fit > 0.5);

        /* claim well clear of the forbidden region → not flagged. */
        float clear[4] = {0.2f, 0.1f, 0, 0};
        GeoConsistency cc;
        engram_verify_consistency(clear, dim, CTX, NULL, NULL, FORB,
                                  1.0, 0.10, 0.5, 0.0, &cc);
        printf("[consistency] CLR  verdict=%d geo_viol=%d forb_fit=%.4f\n",
               cc.verdict, cc.geo_violation, cc.forbidden_fit);
        ok("claim clear of forbidden NOT flagged", cc.geo_violation == 0 && cc.verdict == GEO_CONSIST_OK);

        /* max-distance constraint: claim too far from context (off-axis, no poles). */
        float far[4] = {0, 8.0f, 0, 0};
        GeoConsistency cd;
        engram_verify_consistency(far, dim, CTX, NULL, NULL, NULL,
                                  1.0, 0.10, 0.5, /*max_distance*/3.0, &cd);
        printf("[consistency] DIST verdict=%d geo_viol=%d ctx_dist=%.3f\n",
               cd.verdict, cd.geo_violation, cd.context_distance);
        ok("claim beyond max_distance FLAGGED", cd.geo_violation == 1 && cd.verdict == GEO_CONSIST_GEOMETRIC);
        approx("context distance measured correctly", cd.context_distance, 8.0, 1e-4);

        engram_geo_free(CTX); engram_geo_free(FORB);
    }

    /* ══════════════════ COMBINED — grounded but INVERTED (the full plausible lie) */
    /* The most dangerous output: fluent, GROUNDED in real vocabulary, yet polarity-
     * inverted. Grounding alone passes it; only consistency catches the lie. This is
     * exactly why the verifier needs BOTH checks. */
    {
        int dim = 4;
        double c_pos[4] = { 5, 0, 0, 0}, c_neg[4] = {-5, 0, 0, 0};
        double ax[4] = {1,0,0,0}; double ex[1] = {2};
        const char* ip[1]={"pos"},*in[1]={"neg"};
        GeoDescriptor* POS = mk(dim, c_pos, 1, ax, ex, 1, ip, -1);
        GeoDescriptor* NEG = mk(dim, c_neg, 1, ax, ex, 1, in, -1);
        const GeoDescriptor* ev[2] = {POS, NEG};

        float lie[4] = {5, 0, 0, 0};   /* "argued" — sits dead-center in the affirm region */
        GeoGrounding g;
        engram_verify_grounding(lie, dim, ev, 2, 1.0, 0.5, &g);
        GeoConsistency c;
        engram_verify_consistency(lie, dim, NEG /*truth=never fought*/, POS, NEG, NULL,
                                  1.0, 0.10, 0.5, 0.0, &c);
        printf("[combined] grounded=%d (score=%.3f)  inverted=%d verdict=%d\n",
               g.grounded, g.grounding, c.inverted, c.verdict);
        ok("plausible lie PASSES grounding (it is real vocabulary)", g.grounded == 1);
        ok("plausible lie is CAUGHT by consistency (inverted)", c.inverted == 1);
        ok("=> grounding alone is insufficient; consistency is the catch",
           g.grounded == 1 && c.verdict == GEO_CONSIST_POLARITY);
        engram_verify_grounding_free(&g);
        engram_geo_free(POS); engram_geo_free(NEG);
    }

    printf("\n== %d checks, %d failures ==\n", checks, failures);
    return failures ? 1 : 0;
}
