/**
 * Mission.ts
 * TypeScript definitions for the Mission JSON Schema.
 */

// --- ROOT STRUCTURE ---

export interface Mission {
  /** Unique Mission ID (e.g., 'MISSION_ACT1_FINAL') */
  id: string;
  /** Type of mission context */
  type: MissionType;
  /** Meta-data about difficulty and display */
  config: MissionConfig;
  /** Instructions for Procedural Generation */
  biome: MissionBiome;
  /** Constraints on squad selection */
  deployment?: DeploymentConstraints;
  /** Hooks for Narrative sequences and scripts */
  narrative?: MissionNarrative;
  /** Enemy units to spawn at mission start */
  enemy_spawns?: EnemySpawn[];
  /** Win/Loss conditions */
  objectives: MissionObjectives;
  /** Global rules or stat changes */
  modifiers?: MissionModifier[];
  /** Payouts for success */
  rewards: MissionRewards;
}

export type MissionType = "STORY" | "SIDE_QUEST" | "PROCEDURAL" | "TUTORIAL";

// --- CONFIGURATION ---

export interface MissionConfig {
  title: string;
  description: string;
  /** 1-5 Scale */
  difficulty_tier: number;
  /** Suggested level for Explorers */
  recommended_level?: number;
  /** Path to icon image */
  icon?: string;
  /** List of mission IDs that must be completed before this mission is available */
  prerequisites?: string[];
  /** 
   * Controls visibility when prerequisites are not met.
   * - "hidden": Mission is completely hidden until prerequisites are met (default for STORY)
   * - "locked": Mission shows as locked with requirements visible (default for SIDE_QUEST, PROCEDURAL)
   */
  visibility_when_locked?: "hidden" | "locked";
}

// --- BIOME / WORLD GEN ---

export type BiomeType =
  | "BIOME_FUNGAL_CAVES"
  | "BIOME_RUSTING_WASTES"
  | "BIOME_CRYSTAL_SPIRES"
  | "BIOME_VOID_SEEP"
  | "BIOME_CONTESTED_FRONTIER";

export type SeedType = "RANDOM" | "FIXED";

export interface MissionBiome {
  type: BiomeType;
  generator_config: {
    seed_type: SeedType;
    /** If FIXED, use this seed */
    seed?: number;
    /** Dimensions of the grid */
    size: { x: number; y: number; z: number };
    /** Number of rooms for Ruin generators */
    room_count?: number;
    /** General density modifier */
    density?: "LOW" | "MEDIUM" | "HIGH";
  };
  /** List of environmental hazards to enable (e.g., 'HAZARD_ACID_POOLS') */
  hazards?: string[];
}

// --- DEPLOYMENT ---

export interface DeploymentConstraints {
  /** Max units allowed (Default: 4) */
  squad_size_limit?: number;
  /** IDs of units that MUST be included */
  forced_units?: string[];
  /** IDs of classes that cannot be selected */
  banned_classes?: string[];
  /** IDs of classes/units suggested for this mission (for tutorials) */
  suggested_units?: string[];
  /** Tutorial hint text to display during deployment phase */
  tutorial_hint?: string;
}

// --- ENEMY SPAWNS ---

export interface EnemySpawn {
  /** Enemy definition ID (e.g., 'ENEMY_SHARDBORN_SENTINEL') */
  enemy_def_id: string;
  /** Number of this enemy type to spawn */
  count: number;
}

// --- NARRATIVE & SCRIPTS ---

export interface MissionNarrative {
  /** Narrative ID to play before deployment */
  intro_sequence?: string;
  /** Narrative ID to play upon victory */
  outro_success?: string;
  /** Narrative ID to play upon defeat */
  outro_failure?: string;
  /** Triggers that fire during gameplay */
  scripted_events?: ScriptedEvent[];
}

export interface ScriptedEvent {
  trigger: EventTriggerType;
  /** Specific turn number if trigger is ON_TURN_START */
  turn_index?: number;
  /** Tag or ID if trigger is ON_UNIT_DEATH */
  target_tag?: string;
  /** The action to perform */
  action: "PLAY_SEQUENCE" | "SPAWN_REINFORCEMENTS" | "MODIFY_OBJECTIVE";
  /** ID of the narrative or wave definition */
  sequence_id?: string;
  wave_id?: string;
}

export type EventTriggerType =
  | "ON_TURN_START"
  | "ON_UNIT_DEATH"
  | "ON_UNIT_SPAWN"
  | "ON_HEALTH_PERCENT";

// --- OBJECTIVES ---

export type ObjectiveType =
  | "ELIMINATE_ALL"
  | "ELIMINATE_UNIT"
  | "INTERACT"
  | "REACH_ZONE"
  | "SURVIVE"
  | "ESCORT"
  | "COMPLETE_BEFORE_TURN"
  | "SQUAD_SURVIVAL";

export interface ObjectiveDefinition {
  id: string;
  type: ObjectiveType;
  description: string;
  /** For ELIMINATE_UNIT or ESCORT */
  target_def_id?: string;
  /** For INTERACT */
  target_object_id?: string;
  /** For REACH_ZONE */
  zone_coords?: { x: number; y: number; z: number };
  /** For SURVIVE or COMPLETE_BEFORE_TURN */
  turn_limit?: number;
  /** For ELIMINATE_ALL or SQUAD_SURVIVAL */
  target_count?: number;
  min_alive?: number;
}

export type FailureType = "SQUAD_WIPE" | "VIP_DEATH" | "TURN_LIMIT_EXCEEDED";

export interface FailureCondition {
  type: FailureType;
  target_tag?: string;
}

export interface MissionObjectives {
  /** All must be completed to win */
  primary: ObjectiveDefinition[];
  /** Optional bonus goals */
  secondary?: ObjectiveDefinition[];
  /** Explicit lose conditions */
  failure_conditions?: FailureCondition[];
}

// --- MODIFIERS ---

export type ModifierType = "GLOBAL_EFFECT" | "STAT_MODIFIER";

export interface MissionModifier {
  type: ModifierType;
  description?: string;
  /** For GLOBAL_EFFECT */
  effect_id?: string;
  /** For STAT_MODIFIER */
  target_team?: "PLAYER" | "ENEMY";
  stat?: string;
  value?: number;
}

// --- REWARDS ---

export interface RewardBundle {
  xp?: number;
  currency?: {
    aether_shards?: number;
    ancient_cores?: number;
  };
  /** List of Item IDs */
  items?: string[];
  /** List of Class IDs to unlock */
  unlocks?: string[];
  /** List of Unit IDs or Class IDs to immediately join the roster */
  recruits?: string[];
}

export interface ConditionalReward {
  objective_id: string;
  reward: RewardBundle;
}

export interface MissionRewards {
  guaranteed: RewardBundle;
  conditional?: ConditionalReward[];
  /** Key: Faction ID, Value: Reputation Change */
  faction_reputation?: Record<string, number>;
}
