#ifndef GENESIS_GEN_EVENTS_H
#define GENESIS_GEN_EVENTS_H

#include <stdint.h>
#include "grid.h"

typedef enum {
    EV_GEN_BEGIN = 0,
    EV_ROOM_PROPOSED,
    EV_ROOM_PLACED,
    EV_ROOM_REJECTED,
    EV_DOOR_SITE_CHOSEN,
    EV_CORRIDOR_CARVED,
    EV_LOOP_TESTED,
    EV_LOOP_ACCEPTED,
    EV_CA_ITERATION,
    EV_LAKE_PROPOSED,
    EV_LAKE_REJECTED_IMPASSABLE,
    EV_LAKE_PLACED,
    EV_WREATH_APPLIED,
    EV_BRIDGE_PLACED,
    EV_WALLS_FINISHED,
    EV_STAIRS_PLACED_UP,
    EV_STAIRS_PLACED_DOWN,
    EV_CHOKEPOINT_FOUND,
    EV_MACHINE_PROPOSED,
    EV_BLUEPRINT_CASCADE_BEGIN,
    EV_FEATURE_BUILT,
    EV_MACHINE_ACCEPTED,
    EV_MACHINE_RESTORED_ON_FAIL,
    EV_GEN_END,
    EV_KIND_COUNT
} event_kind_t;

typedef struct {
    uint16_t x;
    uint16_t y;
    cell_t   before;
    cell_t   after;
} cell_diff_t;

typedef struct {
    uint32_t step;
    uint16_t kind;
    uint16_t payload_u16[4];
    int32_t  payload_i32[2];
    uint32_t diff_count;
    cell_diff_t *diffs;
} gen_event_t;

typedef void (*event_sink_fn)(void *ctx, const gen_event_t *ev);

typedef struct {
    event_sink_fn fn;
    void         *ctx;
    uint32_t      step;
} event_sink_t;

void event_emit(event_sink_t *sink, event_kind_t kind,
                const cell_diff_t *diffs, uint32_t diff_count);

const char *event_kind_name(event_kind_t k);
/* Short human title (~20 chars). */
const char *event_kind_what(event_kind_t k);
/* One-line explanation of why this step matters. May be ~120 chars. */
const char *event_kind_why(event_kind_t k);

#endif
