/**
 * @typedef {import("./types.js").UnitStats} UnitStats
 * @typedef {import("./types.js").Position} Position
 * @typedef {import("./types.js").FacingDirection} FacingDirection
 * @typedef {import("./types.js").UnitType} UnitType
 * @typedef {import("./types.js").UnitTeam} UnitTeam
 * @typedef {import("./types.js").StatusEffect} StatusEffect
 */

/**
 * Unit.js
 * Base class for all entities on the grid (Explorers, Enemies, Structures).
 * @class
 */
export class Unit {
  /**
   * @param {string} id - Unique unit identifier
   * @param {string} name - Unit name
   * @param {UnitType} type - Unit type
   * @param {string} voxelModelID - Voxel model identifier
   */
  constructor(id, name, type, voxelModelID) {
    /** @type {string} */
    this.id = id;
    /** @type {string} */
    this.name = name;
    /** @type {UnitType} */
    this.type = type; // 'EXPLORER', 'ENEMY', 'STRUCTURE'
    /** @type {string} */
    this.voxelModelID = voxelModelID;

    // Grid State
    /** @type {Position} */
    this.position = { x: 0, y: 0, z: 0 };
    /** @type {FacingDirection} */
    this.facing = "NORTH";

    // Combat State
    /** @type {number} */
    this.currentHealth = 100;
    /** @type {number} */
    this.maxHealth = 100; // Derived from effective stats later

    /** @type {number} */
    this.currentAP = 0; // Action Points for current turn
    /** @type {number} */
    this.chargeMeter = 0; // Dynamic Initiative (0-100)

    /** @type {StatusEffect[]} */
    this.statusEffects = []; // Active debuffs/buffs

    // Base Stats (Raw values before gear/buffs)
    /** @type {UnitStats} */
    this.baseStats = {
      health: 100,
      attack: 10,
      defense: 5,
      magic: 0,
      speed: 10,
      willpower: 5,
      movement: 4,
      tech: 0,
    };

    /** @type {UnitTeam | undefined} */
    this.team = undefined;
  }

  /**
   * Updates position.
   * Note: Validation happens in VoxelGrid/MovementSystem.
   * @param {number} x - X coordinate
   * @param {number} y - Y coordinate
   * @param {number} z - Z coordinate
   */
  setPosition(x, y, z) {
    this.position = { x, y, z };
  }

  /**
   * Consumes AP. Returns true if successful.
   * @param {number} amount - Amount of AP to spend
   * @returns {boolean} - True if successful
   */
  spendAP(amount) {
    if (this.currentAP >= amount) {
      this.currentAP -= amount;
      return true;
    }
    return false;
  }

  /**
   * Checks if the unit is alive.
   * @returns {boolean} - True if alive
   */
  isAlive() {
    return this.currentHealth > 0;
  }
}
