/**
 * Type definitions for core game systems
 */

import type { GameLoop } from "./GameLoop.js";
import type { RosterManager } from "../managers/RosterManager.js";
import type { MissionManager } from "../managers/MissionManager.js";
import type { NarrativeManager } from "../managers/NarrativeManager.js";
import type { Persistence } from "./Persistence.js";

/**
 * Game state constants
 */
export type GameState =
  | "STATE_INIT"
  | "STATE_MAIN_MENU"
  | "STATE_TEAM_BUILDER"
  | "STATE_DEPLOYMENT"
  | "STATE_COMBAT";

/**
 * Run data structure for active game sessions
 */
export interface RunData {
  id: string;
  missionId: string;
  seed: number;
  depth: number;
  biome: BiomeConfig;
  squad: SquadMember[];
  objectives: Objective[];
  world_state: Record<string, unknown>;
}

/**
 * Squad member definition
 */
export interface SquadMember {
  id?: string;
  classId?: string;
  name?: string;
  isNew?: boolean;
  [key: string]: unknown;
}

/**
 * Biome configuration
 */
export interface BiomeConfig {
  generator_config: {
    seed_type: "FIXED" | "RANDOM";
    seed?: number;
    [key: string]: unknown;
  };
  [key: string]: unknown;
}

/**
 * Objective definition
 */
export interface Objective {
  type: string;
  target_count?: number;
  target_def_id?: string;
  current?: number;
  complete?: boolean;
  [key: string]: unknown;
}

/**
 * Game state change event detail
 */
export interface GameStateChangeDetail {
  oldState: GameState;
  newState: GameState;
  payload: unknown;
}

/**
 * Embark event detail
 */
export interface EmbarkEventDetail {
  mode: "DRAFT" | "DEPLOY";
  squad: SquadMember[];
}

/**
 * GameStateManager class interface
 */
export interface GameStateManagerInterface {
  currentState: GameState;
  gameLoop: GameLoop | null;
  persistence: Persistence;
  activeRunData: RunData | null;
  rosterManager: RosterManager;
  missionManager: MissionManager;
  narrativeManager: NarrativeManager;
  gameLoopInitialized: Promise<void>;
  rosterLoaded: Promise<unknown>;
  setGameLoop(loop: GameLoop): void;
  reset(): void;
  init(): Promise<void>;
  transitionTo(newState: GameState, payload?: unknown): Promise<void>;
  startNewGame(): void;
  continueGame(): Promise<void>;
  handleEmbark(e: CustomEvent<EmbarkEventDetail>): Promise<void>;
}

export type CombatPhase =
  | "INIT"
  | "TURN_START"
  | "WAITING_FOR_INPUT"
  | "EXECUTING_ACTION"
  | "TURN_END"
  | "COMBAT_END";

export interface CombatState {
  /** Whether combat is currently active */
  isActive: boolean;
  /** Current Round number */
  round: number;
  /** Ordered list of Unit IDs for the current round */
  turnQueue: string[];
  /** The ID of the unit currently taking their turn */
  activeUnitId: string | null;
  /** Current phase of the turn loop */
  phase: CombatPhase;
}

// src/types/Actions.ts

export interface ActionRequest {
  type: "MOVE" | "SKILL" | "ITEM" | "WAIT";
  sourceId: string;
  targetId?: string; // For targeted skills
  targetPosition?: { x: number; y: number; z: number }; // For movement/AoE
  skillId?: string;
  itemId?: string;
}

export interface MovementResult {
  success: boolean;
  path: { x: number; y: number; z: number }[];
  costAP: number;
  finalPosition: { x: number; y: number; z: number };
}
