#ifndef JNR_PROJECTILE_H
#define JNR_PROJECTILE_H

#include "engine/entity.h"
#include "engine/camera.h"
#include "engine/tilemap.h"
#include "engine/animation.h"

/* ── Projectile behavior flags ─────────────────────── */
#define PROJ_FROM_PLAYER  (1 << 0)  /* owned by player (vs enemy)        */
#define PROJ_IMPACT       (1 << 1)  /* currently playing impact anim     */
#define PROJ_PIERCING     (1 << 2)  /* passes through enemies            */
#define PROJ_BOUNCY       (1 << 3)  /* bounces off walls                 */
#define PROJ_GRAVITY      (1 << 4)  /* affected by gravity               */
#define PROJ_HOMING       (1 << 5)  /* tracks nearest target             */

/* ── Projectile definition (weapon type) ───────────── */
/* Describes how a class of projectile behaves.          */
/* These are static data - shared by all instances.      */
typedef struct ProjectileDef {
    const char    *name;           /* e.g. "plasma", "rocket"           */
    float          speed;          /* travel speed (px/s)               */
    int            damage;         /* damage per hit                    */
    float          lifetime;       /* seconds before auto-destroy       */
    float          gravity_scale;  /* 0 = no gravity, 1 = full          */
    int            pierce_count;   /* 0 = destroy on first hit          */
    int            bounce_count;   /* 0 = destroy on wall hit           */
    float          homing_strength;/* 0 = none, higher = tighter turn   */
    float          hitbox_w;       /* collision box width               */
    float          hitbox_h;       /* collision box height              */
    uint32_t       flags;          /* PROJ_* behavior flags             */
    const AnimDef *anim_fly;       /* animation while flying            */
    const AnimDef *anim_impact;    /* animation on hit (NULL = instant) */
} ProjectileDef;

/* ── Per-instance data ─────────────────────────────── */
typedef struct ProjectileData {
    const ProjectileDef *def;      /* shared definition                 */
    uint32_t proj_flags;           /* runtime flags (PROJ_FROM_PLAYER, PROJ_IMPACT) */
    float    lifetime;             /* remaining lifetime                */
    int      pierces_left;         /* remaining pierces                 */
    int      bounces_left;         /* remaining bounces                 */
} ProjectileData;

/* ── Built-in weapon definitions ───────────────────── */
extern const ProjectileDef WEAPON_PLASMA;       /* player default       */
extern const ProjectileDef WEAPON_SPREAD;       /* 3-way fan            */
extern const ProjectileDef WEAPON_LASER;        /* fast, piercing       */
extern const ProjectileDef WEAPON_ROCKET;       /* slow, high damage    */
extern const ProjectileDef WEAPON_BOUNCE;       /* ricochets off walls  */
extern const ProjectileDef WEAPON_ENEMY_FIRE;   /* enemy fireball       */

/* ── API ───────────────────────────────────────────── */
void projectile_register(EntityManager *em);

/* Spawn a projectile from a definition + direction vector */
Entity *projectile_spawn_def(EntityManager *em, const ProjectileDef *def,
                             Vec2 pos, Vec2 dir, bool from_player);

/* Convenience: spawn with default player/enemy weapon */
Entity *projectile_spawn_dir(EntityManager *em, Vec2 pos, Vec2 dir, bool from_player);

/* Legacy convenience: horizontal shot */
Entity *projectile_spawn(EntityManager *em, Vec2 pos, bool facing_left, bool from_player);

/* Trigger impact on a projectile (used by collision system) */
void projectile_hit(Entity *proj);

/* Check if a projectile is currently impacting (not collidable) */
bool projectile_is_impacting(const Entity *proj);

/* Check if a projectile belongs to the player */
bool projectile_is_from_player(const Entity *proj);

#endif /* JNR_PROJECTILE_H */
