/**
 * Effects.ts
 * Type definitions for the Game Logic Engine (Effects, Passives, and Triggers).
 */

// =============================================================================
// 1. EFFECT DEFINITIONS (The "What")
// =============================================================================

/**
 * List of all valid actions the EffectProcessor can execute.
 */
export type EffectType =
  // Combat
  | "DAMAGE"
  | "HEAL"
  | "CHAIN_DAMAGE"
  | "REDIRECT_DAMAGE"
  | "PREVENT_DEATH"

  // Status & Stats
  | "APPLY_STATUS"
  | "REMOVE_STATUS"
  | "REMOVE_ALL_DEBUFFS"
  | "APPLY_BUFF"
  | "GIVE_AP"
  | "ADD_CHARGE"
  | "ADD_SHIELD"
  | "CONVERT_DAMAGE_TO_HEAL"
  | "DYNAMIC_BUFF"

  // Movement & Physics
  | "TELEPORT"
  | "MOVE_TO_TARGET"
  | "SWAP_POSITIONS"
  | "PHYSICS_PULL"
  | "PUSH"

  // World & Spawning
  | "SPAWN_OBJECT"
  | "SPAWN_HAZARD"
  | "SPAWN_LOOT"
  | "MODIFY_TERRAIN"
  | "DESTROY_VOXEL"
  | "DESTROY_OBJECTS"
  | "REVEAL_OBJECTS"
  | "COLLECT_LOOT"

  // Meta / Logic
  | "REPEAT_SKILL"
  | "CANCEL_EVENT"
  | "REDUCE_COST"
  | "BUFF_SPAWN"
  | "MODIFY_AOE";

/**
 * A generic container for parameters used by Effect Handlers.
 * In a strict system, this would be a Union of specific interfaces,
 * but for JSON deserialization, a loose interface is often more flexible.
 */
export interface EffectParams {
  // -- Combat Magnitude --
  power?: number; // Base amount (Damage/Heal)
  attribute?: string; // Stat to scale off (e.g., "strength", "magic")
  scaling?: number; // Multiplier for attribute (Default: 1.0)
  element?: "PHYSICAL" | "FIRE" | "ICE" | "SHOCK" | "VOID" | "TECH";

  // -- Chaining --
  bounces?: number;
  decay?: number;
  synergy_trigger?: string; // Status ID that triggers bonus effect

  // -- Status/Buffs --
  status_id?: string;
  duration?: number;
  stat?: string; // For Buffs
  value?: number; // For Buffs/Mods
  chance?: number; // 0.0 to 1.0 (Proc chance)

  // -- Physics --
  force?: number; // Distance
  destination?: "TARGET" | "BEHIND_TARGET" | "ADJACENT_TO_TARGET";

  // -- World --
  object_id?: string; // Unit ID to spawn
  hazard_id?: string;
  tag?: string; // Filter for objects (e.g. "COVER")
  range?: number; // AoE radius

  // -- Logic --
  percentage?: number; // 0.0 - 1.0
  amount?: number; // Flat amount (AP/Charge)
  amount_range?: [number, number]; // [min, max]
  set_hp?: number; // Hard set HP value
  shape?: "CIRCLE" | "LINE" | "CONE" | "SINGLE";
  size?: number;
  multiplier?: number;
}

/**
 * The runtime payload passed to EffectProcessor.process().
 * Combines the Type and the Params.
 */
export interface EffectDefinition extends EffectParams {
  type: EffectType;

  // Optional Override Condition for the Effect itself
  // (Distinct from the Passive Trigger condition)
  condition?: ConditionDefinition;

  // Conditional Multiplier (e.g. Execute damage)
  conditional_multiplier?: {
    condition: string; // Condition Tag
    value: number;
  };
}

// =============================================================================
// 2. PASSIVE DEFINITIONS (The "When")
// =============================================================================

/**
 * Triggers that the EventSystem listens for.
 */
export type TriggerType =
  // Stat Calculation Hooks
  | "ON_STAT_CALC"
  | "STATIC_STAT_MOD"
  | "ON_SKILL_COST_CALC"
  | "ON_ATTACK_CALC"
  | "ON_DAMAGE_CALC"

  // Combat Events
  | "ON_ATTACK_HIT"
  | "ON_SKILL_HIT"
  | "ON_SKILL_CAST"
  | "ON_DAMAGED"
  | "ON_ALLY_DAMAGED"
  | "ON_DAMAGE_DEALT"
  | "ON_HEAL_DEALT"
  | "ON_HEAL_OVERFLOW"
  | "ON_KILL"
  | "ON_LETHAL_DAMAGE"
  | "ON_SYNERGY_TRIGGER"

  // Turn Lifecycle
  | "ON_TURN_START"
  | "ON_TURN_END"
  | "ON_ACTION_COMPLETE"

  // World Events
  | "ON_MOVE_COMPLETE"
  | "ON_HAZARD_TICK"
  | "ON_TRAP_TRIGGER"
  | "ON_OBJECT_DESTROYED"
  | "ON_SPAWN_OBJECT"
  | "ON_LEVEL_START"

  // Meta / UI
  | "GLOBAL_SHOP_PRICE"
  | "ON_SKILL_TARGETING"
  | "AURA_UPDATE";

/**
 * How a stat modifier interacts with the base value.
 */
export type ModifierType =
  | "ADD"
  | "MULTIPLY"
  | "ADD_PER_ADJACENT_ENEMY"
  | "ADD_PER_DESTROYED_VOXEL"
  | "ADD_STAT"
  | "MULTIPLY_DAMAGE";

/**
 * An entry in `passive_registry.json`.
 * Defines a rule: "WHEN [Trigger] IF [Condition] THEN [Action]".
 */
export interface PassiveDefinition {
  id: string;
  name: string;
  description: string;

  // -- The Trigger --
  trigger: TriggerType;

  // -- The Check --
  condition?: ConditionDefinition;
  limit?: number; // Max times per run/turn

  // -- The Effect (Action) --
  // Maps to EffectDefinition.type
  action?: EffectType;

  // -- The Parameters --
  // Merged into EffectDefinition
  params?: EffectParams;

  // -- For Stat Modifiers (simpler than full Effects) --
  stat?: string;
  modifier_type?: ModifierType;
  value?: number;
  range?: number; // For Aura/Per-Adjacent checks

  // -- For Complex Stat Lists --
  modifiers?: { stat: string; value: number }[];

  // -- For Auras --
  target?: "ALLIES" | "ENEMIES" | "SELF";
}

// =============================================================================
// 3. CONDITIONS (The "If")
// =============================================================================

export type ConditionType =
  | "SOURCE_IS_ADJACENT"
  | "IS_ADJACENT"
  | "IS_BASIC_ATTACK"
  | "SKILL_TAG" // value: string
  | "DAMAGE_TYPE" // value: string
  | "TARGET_TAG" // value: string
  | "OWNER_IS_SELF"
  | "IS_FLANKING"
  | "TARGET_LOCKED"
  | "DID_NOT_ATTACK"
  | "TARGET_HP_LOW"
  | "IS_MECHANICAL";

export interface ConditionDefinition {
  type: ConditionType;
  value?: string | number | boolean;
}
