export type EntityId = number;

export type Vec2 = { x: number; y: number };

export type Tile = number;
export type EnemyType = "rat" | "bat";
export type ActorType = "player" | EnemyType;

export type EnemyAIState = "wandering" | "alerted" | "pursuing";

export type Action =
  | { type: "move"; dx: number; dy: number }
  | { type: "attack"; targetId: EntityId }
  | { type: "wait" };

export type SimEvent =
  | { type: "moved"; actorId: EntityId; from: Vec2; to: Vec2 }
  | { type: "attacked"; attackerId: EntityId; targetId: EntityId }
  | { type: "damaged"; targetId: EntityId; amount: number; hp: number; x: number; y: number; isCrit?: boolean; isBlock?: boolean }
  | { type: "dodged"; targetId: EntityId; x: number; y: number }
  | { type: "healed"; actorId: EntityId; amount: number; x: number; y: number }
  | { type: "killed"; targetId: EntityId; killerId: EntityId; x: number; y: number; victimType?: ActorType }

  | { type: "waited"; actorId: EntityId }
  | { type: "exp-collected"; actorId: EntityId; amount: number; x: number; y: number }
  | { type: "orb-spawned"; orbId: EntityId; x: number; y: number }
  | { type: "leveled-up"; actorId: EntityId; level: number; x: number; y: number }
  | { type: "enemy-alerted"; enemyId: EntityId; x: number; y: number };


export type Stats = {
  maxHp: number;
  hp: number;
  maxMana: number;
  mana: number;
  attack: number;
  defense: number;
  level: number;
  exp: number;
  expToNextLevel: number;

  // Offensive
  critChance: number;
  critMultiplier: number;
  accuracy: number;
  lifesteal: number;

  // Defensive
  evasion: number;
  blockChance: number;

  // Utility
  luck: number;

  // New Progression Fields
  statPoints: number;
  skillPoints: number;
  strength: number;
  dexterity: number;
  intelligence: number;
  passiveNodes: string[]; // List of IDs for allocated passive nodes
};


export type ItemType = 
  | "Weapon" 
  | "Offhand" 
  | "BodyArmour" 
  | "Helmet" 
  | "Gloves" 
  | "Boots" 
  | "Amulet" 
  | "Ring" 
  | "Belt" 
  | "Currency";

export type Item = {
  id: string;
  name: string;
  type: ItemType;
  stats?: Partial<Stats>;
  icon?: string;
};

export type Equipment = {
  mainHand?: Item;
  offHand?: Item;
  bodyArmour?: Item;
  helmet?: Item;
  gloves?: Item;
  boots?: Item;
  amulet?: Item;
  ringLeft?: Item;
  ringRight?: Item;
  belt?: Item;
};

export type Inventory = {
  gold: number;
  items: Item[];
};

export type RunState = {
  stats: Stats;
  inventory: Inventory;
};

export interface BaseActor {
  id: EntityId;
  pos: Vec2;
  type?: string;
}

export interface CombatantActor extends BaseActor {
  category: "combatant";
  isPlayer: boolean;
  type: ActorType;
  speed: number;
  stats: Stats;
  inventory?: Inventory;
  equipment?: Equipment;
  
  // Enemy AI state
  aiState?: EnemyAIState;
  alertedAt?: number;
  lastKnownPlayerPos?: Vec2;
}

export interface CollectibleActor extends BaseActor {
  category: "collectible";
  type: "exp_orb";
  expAmount: number;
}

export interface ItemActor extends BaseActor {
  category: "item";
  item: Item;
}

export type Actor = CombatantActor | CollectibleActor | ItemActor;

export type World = {
  width: number;
  height: number;
  tiles: Tile[];
  actors: Map<EntityId, Actor>;
  exit: Vec2;
};
