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

// --- ROOT STRUCTURE ---

export interface NarrativeSequence {
  /** Unique Sequence ID (e.g., 'NARRATIVE_TUTORIAL_INTRO') */
  id: string;
  /** Ordered list of narrative nodes */
  nodes: NarrativeNode[];
}

// --- NODE TYPES ---

export type NarrativeNodeType = "DIALOGUE" | "CHOICE" | "TUTORIAL" | "ACTION";

/** Union type of all possible node configurations */
export type NarrativeNode =
  | DialogueNode
  | ChoiceNode
  | TutorialNode
  | ActionNode;

/** Common fields shared by all nodes */
interface BaseNode {
  /** Unique string identifier within this sequence */
  id: string;
  type: NarrativeNodeType;
  /** Optional side-effect triggered when entering this node */
  trigger?: NarrativeTrigger;
  /** ID of the next node. If 'END' or undefined, sequence finishes. */
  next?: string;
}

/** * Standard conversation node.
 * Displays text, a speaker name, and an optional portrait.
 */
export interface DialogueNode extends BaseNode {
  type: "DIALOGUE";
  speaker: string;
  text: string;
  /** Path to image asset */
  portrait?: string;
  /** Path to audio file */
  voice_clip?: string;
}

/** * Branching path node.
 * Presents a list of buttons to the user.
 */
export interface ChoiceNode extends BaseNode {
  type: "CHOICE";
  speaker: string;
  text: string;
  choices: ChoiceOption[];
}

/** * Tutorial overlay node.
 * Highlights UI elements and optionally blocks input.
 */
export interface TutorialNode extends BaseNode {
  type: "TUTORIAL";
  speaker: string;
  text: string;
  /** CSS Selector string to visually highlight/pulse (e.g., '#btn-end-turn') */
  highlight_selector?: string;
  /** Explicit screen coordinates if selector is insufficient */
  highlight_rect?: { x: number; y: number; w: number; h: number };
  /** If true, prevents clicking anything except the highlighted element */
  block_input?: boolean;
}

/** * Invisible logic node.
 * Used purely to execute a trigger and immediately jump to next.
 */
export interface ActionNode extends BaseNode {
  type: "ACTION";
  // Action nodes usually rely heavily on the 'trigger' field in BaseNode
}

// --- SUPPORTING TYPES ---

export interface ChoiceOption {
  text: string;
  /** Target Node ID */
  next: string;
  /** Optional side-effect specific to this choice */
  trigger?: NarrativeTrigger;
  /** Logic check. If false, choice is hidden or disabled */
  condition?: NarrativeCondition;
}

export type TriggerType =
  | "SPAWN_WAVE"
  | "START_DEPLOYMENT"
  | "GIVE_ITEM"
  | "MODIFY_RELATION"
  | "PLAY_SFX"
  | "GIVE_AP"
  | "APPLY_BUFF";

export interface NarrativeTrigger {
  type: TriggerType;
  // Dynamic params based on type
  wave_id?: string;
  item_id?: string;
  faction?: string;
  amount?: number;
  file_path?: string;
  stat?: string;
  value?: number;
  [key: string]: any;
}

export type ConditionType = "HAS_CLASS" | "HAS_ITEM" | "STAT_CHECK";

export interface NarrativeCondition {
  type: ConditionType;
  value?: string | number;
  stat?: string;
}
