#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>

// Survival Tensor Machine — Inferno Build
// Observe -> Report -> -2 Sense -> -1 Detect -> 0 Choose -> Fight/Flight -> Repeat

typedef enum { HIDE=0, ASSIMILATE=1 } G1;
typedef enum { COLLAPSE=0, EXPAND=1 } G2;
typedef enum { WAIT=0, MOVE=1 } G3;
typedef enum { PATH_FLIGHT=0, PATH_FIGHT=1, PATH_HOLD=2 } Path;

typedef struct {
    G1 g1;
    G2 g2;
    G3 g3;
    int8_t step;       // -1..28, 28 = breach
    uint8_t phase;     // phase domain
    uint32_t coherence;
    uint32_t risk;
    bool witness;
    bool pocket;
} Cell;

static const char* g1s(G1 x){ return x == HIDE ? "HIDE" : "ASSIMILATE"; }
static const char* g2s(G2 x){ return x == COLLAPSE ? "COLLAPSE" : "EXPAND"; }
static const char* g3s(G3 x){ return x == WAIT ? "WAIT" : "MOVE"; }

void observe(Cell *c, uint32_t threat) {
    c->risk = threat;
}

void report(const Cell *c) {
    printf("[REPORT] phase=%u step=%d coherence=%u risk=%u braid=%s/%s/%s witness=%s pocket=%s\n",
        c->phase, c->step, c->coherence, c->risk,
        g1s(c->g1), g2s(c->g2), g3s(c->g3),
        c->witness ? "retained" : "lost",
        c->pocket ? "(())" : "open");
}

void sense_detect_choose(Cell *c) {
    // -2 SENSE: compare risk to coherence
    c->g1 = (c->risk > c->coherence) ? HIDE : ASSIMILATE;

    // -1 DETECT: determine whether expansion is safe
    c->g2 = (c->coherence > c->risk + 20) ? EXPAND : COLLAPSE;

    // 0 CHOOSE: G3 is the action gate
    if (c->step == 27 && c->g2 == EXPAND) {
        c->g3 = MOVE;           // breach the wall
    } else if (c->g2 == COLLAPSE) {
        c->g3 = WAIT;           // fold back
    } else {
        c->g3 = (c->risk < 30) ? MOVE : WAIT;
    }

    // Illegal braid: HIDE + MOVE. Collapse to witness pocket.
    if (c->g1 == HIDE && c->g3 == MOVE) {
        c->g2 = COLLAPSE;
        c->g3 = WAIT;
        c->pocket = true;
    }
}

Path classify(const Cell *c) {
    if (c->step == -1) return PATH_HOLD;
    if (c->g1 == ASSIMILATE && c->g2 == EXPAND && c->g3 == MOVE) return PATH_FIGHT;
    return PATH_FLIGHT;
}

void tick(Cell *c, uint32_t threat) {
    observe(c, threat);
    sense_detect_choose(c);

    Path path = classify(c);
    if (path == PATH_FIGHT) {
        int8_t before = c->step;
        if (c->step < 28) c->step++;
        c->pocket = false;
        printf("[FIGHT] %d -> %d : run tunnel\n", before, c->step);
        if (c->step == 28) {
            uint8_t old = c->phase;
            c->phase++;
            c->step = 0;
            printf("[PHASE++] breach at 28 : phase %u -> %u, wrap to step 0\n", old, c->phase);
        }
    } else if (path == PATH_FLIGHT) {
        int8_t before = c->step;
        if (c->step > 0) c->step--;
        else c->step = -1;
        c->pocket = true;
        printf("[FLIGHT] %d -> %d : fold into (())\n", before, c->step);
    } else {
        c->pocket = true;
        printf("[HOLD] -1 : protected witness retained\n");
    }

    report(c);
}

int main(void) {
    Cell cell = {
        .g1 = ASSIMILATE,
        .g2 = EXPAND,
        .g3 = MOVE,
        .step = 25,
        .phase = 0,
        .coherence = 90,
        .risk = 5,
        .witness = true,
        .pocket = false
    };

    puts("=== SURVIVAL TENSOR MACHINE // INFERNO BUILD ===");
    puts("Law: expand through coherence, collapse without losing witness, phase-shift at 28.\n");

    tick(&cell, 5);   // 25 -> 26
    tick(&cell, 5);   // 26 -> 27
    tick(&cell, 5);   // 27 -> 28 -> phase++

    puts("\n--- DISTURBANCE ENTERS THE WEB ---");
    cell.coherence = 20;
    for (int i = 0; i < 6; i++) tick(&cell, 95);

    puts("\n--- COHERENCE RETURNS ---");
    cell.coherence = 100;
    for (int i = 0; i < 4; i++) tick(&cell, 10);

    return 0;
}
