#ifndef JNR_ENTITY_REGISTRY_H
#define JNR_ENTITY_REGISTRY_H

#include "engine/entity.h"
#include <SDL2/SDL.h>

/* ═══════════════════════════════════════════════════
 *  Entity Registry
 *
 *  Central table that maps entity type name strings
 *  to their spawn functions and metadata. Used by:
 *  - Level loader (to spawn entities from .lvl files)
 *  - Level editor (to discover available entity types)
 *
 *  When adding a new entity type to the game, add one
 *  entry here and it will appear in the editor palette
 *  automatically.
 * ═══════════════════════════════════════════════════ */

#define MAX_REGISTRY_ENTRIES 32

/* Spawn function: creates an entity at position, returns it */
typedef Entity *(*EntitySpawnFn)(EntityManager *em, Vec2 pos);

/* ── Editor mini-icon indices ────────────────────── */
/* Shared between the editor (bitmap definitions) and
 * the registry (icon field in each entry).  Keep in
 * sync with s_icon_bitmaps[] in editor.c. */
typedef enum EditorIcon {
    ICON_GRUNT       =  0,  /* spiky enemy        */
    ICON_FLYER       =  1,  /* bat / wing          */
    ICON_TURRET      =  2,  /* cannon              */
    ICON_PLATFORM    =  3,  /* horizontal bar      */
    ICON_PLATFORM_V  =  4,  /* vertical bar        */
    ICON_FLAME       =  5,  /* fire / flame vent   */
    ICON_FORCEFIELD  =  6,  /* electric field      */
    ICON_HEART       =  7,  /* health pickup       */
    ICON_BOLT        =  8,  /* jetpack / lightning  */
    ICON_FUEL        =  9,  /* fuel canister        */
    ICON_DRONE       = 10,  /* drone companion     */
    ICON_GUN         = 11,  /* weapon pickup       */
    ICON_ASTEROID    = 12,  /* rock                */
    ICON_SPACECRAFT  = 13,  /* ship                */
    ICON_LASER       = 14,  /* laser turret        */
    ICON_LASER_TRACK = 15,  /* tracking laser      */
    ICON_CHARGER     = 16,  /* charging enemy      */
    ICON_SPAWNER     = 17,  /* spawner enemy       */
    ICON_COUNT,
    ICON_NONE        = -1   /* no icon (fallback)  */
} EditorIcon;

typedef struct EntityRegEntry {
    const char   *name;         /* .lvl file name, e.g. "grunt"          */
    const char   *display;      /* human-readable, e.g. "Grunt"          */
    EntitySpawnFn spawn_fn;     /* function that creates the entity      */
    SDL_Color     color;        /* editor palette color for preview      */
    int           width;        /* hitbox width  (for editor preview)    */
    int           height;       /* hitbox height (for editor preview)    */
    int           icon;         /* editor mini-icon index (-1 = none)    */
} EntityRegEntry;

typedef struct EntityRegistry {
    EntityRegEntry entries[MAX_REGISTRY_ENTRIES];
    int            count;
} EntityRegistry;

/* Global registry instance */
extern EntityRegistry g_entity_registry;

/* Call once at startup to populate the registry.
 * Also registers entity behaviors with the given EntityManager. */
void entity_registry_init(EntityManager *em);

/* Populate only the registry table (names, colors, icons) without
 * registering entity behaviors. Safe to call without an EntityManager.
 * Used by the editor when it starts without a prior level load. */
void entity_registry_populate(void);

/* Look up a registry entry by name (returns NULL if not found) */
const EntityRegEntry *entity_registry_find(const char *name);

/* Spawn an entity by registry name. Returns NULL if name unknown. */
Entity *entity_registry_spawn(EntityManager *em, const char *name, Vec2 pos);

#endif /* JNR_ENTITY_REGISTRY_H */
